solving a function equal to zero
10 次查看(过去 30 天)
显示 更早的评论
i am trying to run this code to obtain the last value to the function y=0
format loose
format compact
format long
m=[ 4000 50 ] ;
s= [400 5 ] ;
ls=[];
n=length(m) ;
for i = 1 : n
eval(sprintf('syms x%i,',i));
eval(sprintf('x(%i) = x%i;', i, i));
end
Y= @(x1, x2) (29-(6*x(1))-(18*x(2)));
for i=1:n
temp =(-diff(Y,x(i)));
ls=[ls, temp];
end
for i=1:n
if i<n
xi=m(i);
disp(x);
disp(i);
else
last=4000 ;
xi = fzero(Y,last) ;
end
end
i am trying to define the last value of the function y=0 using initial guess but i am getting this error
Error using fzero (line 328)
Function value at starting guess must be finite and real.
0 个评论
采纳的回答
dpb
2019-5-26
编辑:dpb
2019-5-27
fsolve does the work for you...if you define the functional correctly--
fnY= @(x) (29-(6*x(1))-(18*x(2)));
opt= optimoptions('fsolve','algorithm','levenberg-marquardt');
>> fsolve(Y,[0 0],opt)
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the default value of the function tolerance, and
the problem appears regular as measured by the gradient.
<stopping criteria details>
ans =
0.4833 1.4500
>>
Of course, there are an infinite number of possible solutions; pick a value for one or the other of the two X and solve for the other.
2 个评论
dpb
2019-5-27
That's simply
>> x1=4000;
>> x2=(29-(6*x1))/18
x2 =
-1.3317e+03
>>
But, you can still use fsolve if must...there's just one variable to solve for, however...
>> Y= @(x) (29-(6*x1)-(18*x));
>> x2=fsolve(Y,[ 0],opt)
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the default value of the function tolerance, and
the problem appears regular as measured by the gradient.
<stopping criteria details>
x2 =
-1.3317e+03
>>
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Systems of Nonlinear Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!