我有一个方程:
5 + 6x + 7x^1.5 + 4x^2
我能用Matlab计算x的根吗?
谢谢。
是的,您可以使用 fzero
实现 - 只要函数具有非虚根。如果要包含虚根,请参见 @Daniel 的答案
fun = @(x)5+6*x+7*x.^1.5+4*x.^2;
oneRoot = fzero(fun,0); %# find a root starting to look at zero
然而,在进行任何优化问题之前(如果可能的话),通常最好先绘制函数图形,以确定良好的起始值,并查看是否可能会有不好的惊喜,例如在您的情况下没有根。
figure
ezplot(fun,[-10 10])
请注意,由于您的函数为负数的输入参数返回虚数值,因此在x<0时没有数据。此外,该函数似乎严格为正,因此您将找不到任何x
使得y==0
。
如果要得到一个数值解,您需要使用 fminsearch
来查找虚根。
fun = @(x)5+6*x+7*x.^1.5+4*x.^2;
%objective function: fun2 is zero for roots and never negative, searching mins of fun2 for a solution
fun2=@(x)(abs(fun(x(1)+i*x(2))));
oneRoot = sum(fminsearch(fun2,[0,0])*[1,i]);
syms x
eq=5 + 6*x + 7*x^(15/10) + 4*x^2;
sol=solve(eq,x);
vpa(sol,16)
- 0.3923580269955611 + 0.3897288587097241i
- 0.3923580269955611 - 0.3897288587097241i