Matlab code for Jacobian and nonlinear function

1k Views Asked by At

Write the nonlinear system

$x_1^3-2x^2=2$

$x_1^3-5x_3^2=-7$

$x_2x_3^2=1$

in the form $f(x)=0$.

Compute the Jacobian J(x).

Create the files sys.m and sys_jac.m that take as a argument a 3-dimensional column vector $x$ and return $f(x)$ and the Jacobian $J(x)$.

I'm pretty new to matlab, and I also don't understand some of the wording in this request ("sys.m and sys_jac.m that take as a argument a 3-dimensional column vector x and return f(x) and the Jacobian J(x)"

I tried putting in the sys.m

function result=sys(x)

f = inline ( '[x(1)^3-2*x(2)-2 ; x(1)^3-5*x(3)^2+7; x(2)*x(3)^2-1] ');

end

and sys_jac.m

function result = sys_jac(x)


j=jacobian([[x(1)^3-2*x(2)-2 ; x(1)^3-5*x(3)^2+7; x(2)*x(3)^2-1]], [x(1), x(2), x(3)]);

end

but nothing really works and I still don't exactly understand what is the request of this program?

1

There are 1 best solutions below

0
On BEST ANSWER

In Matlab, you define a function like this:

function [x,y,z]=sphe(r,theta,phi)
x=r*cos(theta)*cos(phi)
y=r*sin(theta)*cos(phi)
z=r*sin(phi)
end

If there is only one output, you may write simply

function y=f(x)
y=x*x
end

But you must of course assign a value to the output variable. Then, when you call the function somewhere, you may write

[x,y,z]=sphe(u,v,w)
y=f(10)

(that's from memory, since I have not worked with Matlab since 2004, but it should be correct)