如何在MATLAB中绘制非线性方程?

时间:2014-10-28 12:48:31

标签: matlab math graph plot equation

公式:

0 = x*114 - x*log3(x) - 20.28*y 

我有Y = 10 ^ 3,10 ^ 6,10 ^ 9,10 ^ 12,10 ^ 15,......和上面提到的等式。如何求解(即获得不同y的x值)并在MATLAB中绘制该等式?

1 个答案:

答案 0 :(得分:1)

假设解决方案是唯一的,解决方程的一种方法是数字:

y = 10.^(3:3:15);
opts = optimset( 'TolFun', 1e-9, 'TolX', 1e-9, 'MaxIter', 1e6, 'MaxFunEvals', 1e6 );
for ii = 1 : length(y);
  fcost = @(x)( x*114 - x*log10(x)/log10(3) - 20.28*y(ii) ).^2;
  xopt(ii) = fminsearch( fcost, y(ii), opts );
  fprintf( 1, 'y = %-5g : x = %-17.15g : f(xopt) = %-17.15g\n', ...
    y(ii), xopt(ii), fcost(xopt(ii)) );
end

% Plot the answers on a log-log scale.
loglog( xopt, y, 'k*' );

以上产生以下输出,因此我们可以看到解决方案是有效的。

y = 1000  : x = 185.637624348601  : f(xopt) = 6.37079420013969e-17
y = 1e+06 : x = 197078.905034998  : f(xopt) = 8.88178419700125e-16
y = 1e+09 : x = 210030727.535742  : f(xopt) = 5.82076609134674e-11
y = 1e+12 : x = 224814576366.276  : f(xopt) = 1.52587890625e-05
y = 1e+15 : x = 241850436303022   : f(xopt) = 0                

可能有一种更好的符号方式,但上述方法可行。我只是通过将其设置为y来猜测搜索的起点。如果您需要更准确的解决方案,请查看help optimsethelp fminsearch。您可以传递一些参数来控制解决方案的准确性以及搜索何时终止。