第 0章 :Matlab简介
1. 求解方程组:
的根
方法1:多项式求根函数roots
>> p = [1, -3, 1];
>> x = roots(p)
x =
2.6180
0.3820
>> x = [-5: 0.1: 5];
>> y1 = x.^2 -3*x + 1;
>> y2 = zeros(size(x));
>> plot(x, y1, x, y2);
方法2:fzero
>> f=@(x)x*x - 3*x +1;
>> x1=fzero(f,0.5) //求解靠近0.5的解
x1 =
0.3820
>> x2=fzero(f,2.5) //求解靠近2.5的解
x2 =
2.6180
方法3:求根函数fsolve
>> f=@(x)x*x - 3*x +1;
>> x1 = fsolve(f,0.5,optimset('Display','off'))
x1 =
0.3820
>> x2 = fsolve(f,2.5,optimset('Display','off'))
x2 =
2.6180
方法4;符号求根函数solve
>> syms x
>> x = solve(x.^2 - 3*x +1)
x =
5^(1/2)/2 + 3/2
3/2 - 5^(1/2)/2
>> x = eval(x)
x =
2.6180
0.3820
网友评论