使用MATLAB求的根
方法一
>> p = [1,-3,1];
>> x = roots(p)
x =
2.6180
0.3820
画图
>> x=-5:0.1:5;
>> y1=x.*x - 3*x + 1;
>> y2 = zeros(size(x));
>> plot(x, y1, x, y2)
方法二 函数
fzero
>> f = @(x) x*x - 3*x + 1;
>> x1 = fzero(f, 0.5) # 在x=0.5附近的实根
x1 =
0.3820
>> x2 = fzero(f, 2.5)
x2 =
2.6180
方法三 求根函数
fsolve
>> x1 = fsolve(f, 0.5, optimset('Display', 'off'))
x1 =
0.3820
>> x2 = fsolve(f, 2.5, optimset('Display', 'off'))
x2 =
2.6180
方法四 函数
solve
>> syms x
>> x = solve(x^2 - 3*x + 1)
x =
3/2 - 5^(1/2)/2
5^(1/2)/2 + 3/2
>> eval(x)
ans =
0.3820
2.6180