例?求下列非线性方程组在(0.5,0.5) 附近的数值解。
(1) 建立函数文件myfun.m。
function q=myfun(p)
x=p(1);
y=p(2);
q(1)=x-0.6*sin(x)-0.3*cos(y);
q(2)=y-0.6*cos(x)+0.3*sin(y);
(2) 在给定的初值x0=0.5,y0=0.5下,调用fsolve函数求方程的根。
x=fsolve('myfun',[0.5,0.5]',optimset('Display','off'))
x =
0.6354
0.3734
将求得的解代回原方程,可以检验结果是否正确,命令如下:
q=myfun(x)
q =
1.0e-009 *
0.2375?0.2957
可见得到了较高精度的结果。
cite from:http://blog.sina.com.cn/s/blog_56ef652d0100ebew.html
4、fsolve函数解方程
[X,FVAL,EXITFLAG,OUTPUT,JACOB]=FSOLVE(FUN,X0,...) returns the
Jacobian of FUN at X.
Examples
FUN can be specified using @:
x = fsolve(@myfun,[2 3 4],optimset('Display','iter'))
where myfun is a MATLAB function such as:
function F = myfun(x)
F = sin(x);
FUN can also be an anonymous function:
x = fsolve(@(x) sin(3*x),[1 4],optimset('Display','off'))
If FUN is parameterized, you can use anonymous functions to capture the
problem-dependent parameters. Suppose you want to solve the system of
nonlinear equations given in the function myfun, which is parameterized
by its second argument c. Here myfun is an M-file function such as
function F = myfun(x,c)
F = [ 2*x(1) - x(2) - exp(c*x(1))
-x(1) + 2*x(2) - exp(c*x(2))];
To solve the system of equations for a specific value of c, first assign the
value to c. Then create a one-argument anonymous function that captures
that value of c and calls myfun with two arguments. Finally, pass this anonymous
function to FSOLVE:
c = -1; % define parameter first
x = fsolve(@(x) myfun(x,c),[-5;-5])