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.

采纳的回答

dpb
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 个评论
sarra aloui
sarra aloui 2019-5-27
thank you but the value of x1 is given already as 4000 i am trying to look just for x2
dpb
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 CenterFile Exchange 中查找有关 Systems of Nonlinear Equations 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by