solving a function equal to zero
58 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
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
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
>>
More Answers (0)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!