solving a function equal to zero

58 views (last 30 days)
sarra aloui
sarra aloui on 26 May 2019
Commented: dpb on 27 May 2019
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.

Accepted Answer

dpb
dpb on 26 May 2019
Edited: dpb on 27 May 2019
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 Comments
sarra aloui
sarra aloui on 27 May 2019
thank you but the value of x1 is given already as 4000 i am trying to look just for x2
dpb
dpb on 27 May 2019
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
>>

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!