Using fzero to keep tolerance within limits

12 views (last 30 days)
I would like to get the value of f to zero with Th value changing. Th is my initial guess. Something similar to goalseek in excel. My objective function is x-y which is to be made to zero by changing the value of Th. The error is "FUN must be a function, a valid string expression, or an inline function object." I think using fzero would be easier rather than to write a code for bisection method. Right?
Th=10;
x=100*((0.15*70/Th)-(0.5*0.75));
y=6.205;
f = x-y;
z = fzero(f,Th);

Accepted Answer

dpb
dpb on 19 Jun 2021
As the error message says, you need a function name/handle as the argument to fzero not just an expression.
>> fn=@(T)100*(0.15*70./T-0.375)-6.205
fn =
function_handle with value:
@(T)100*(0.15*70./T-0.375)-6.205
>>
>> T0=10; % initial guess
>> fzero(fn,T0)
ans =
3.8692e-16
>> fn(ans)
ans =
2.7137e+18
>>
Notice this didn't give a reasonable solution; the discontinuity in the function at T==0 screws up the initial search pattern internally looking for a zero-crossing. I'm not sure why fzero didn't throw any errors or warnings here; seems peculiar in that way although I didn't take the time to dig.
>> fzero(fn,[T0 T0+100])
ans =
24.0247
>> fn(ans)
ans =
5.3291e-15
>>
does find a solution; using the range to force it to look on the positive side of T==0 was sufficient help.
I didn't explore just where the critical value for the initial guess is; 20 was close enough; 10 wasn't, so somewhere in between there.
  3 Comments
Reshma B
Reshma B on 20 Jun 2021
Edited: dpb on 20 Jun 2021
Actually this my problem. I had divided it to a small portion. I solved it using vpasolve. Can it be solved using fzero function. My present issue is with the last two lines. It is showing me the answer in terms of Th and not accepting the sol variable.
clear all
clc
Al=2.25;
lw=1.5;
J=2538;
ks=0;
beq=0.75;
qav=39.41;
L=sym(zeros(50,7));
K=0;
j=1;
x=0;
syms Th
for x=0:0.015:0.75
L(j,1)=x;
if K==0
L(j,2)=vpa(-((39.41*1.5^2)/(12*Th))*(8*((x/1.5)^3)-1),10);
L(j,3)=vpa(-(2*39.41*1.5/Th)*(x/1.5)^2,10);
else
K=Al*ks/(x*beq);
a=sqrt(K/Th);
M=(2*exp(-0.5*a*x)+a*x)/(exp(0.5*a*x)+exp(-0.5*a*x));
C = -2*qav/(x*K*a);
L(j,2)= vpa(-C*(M*exp(a*x)+(M-2)*exp(-a*x)-2*a*x),10);
L(j,3) = vpa(-C*a*(M*exp(a*x)-(M-2)*exp(-a*x)-2),10);
end
L(j,4) = vpa(Th*sqrt(1+L(j,3)^2),10);
if j==1
L(j,5)=0;
else
L(j,5) = vpa((L(j,2)-L(j-1,2))/(L(j,1)-L(j-1,1)),10);
end
L(j,6)= vpa(Th*sqrt(1+L(j,5)^2),10);
L(j,7) = vpa(L(j,4)/J,10);
j=j+1;
end
Strsum=sum(L(:,6));
del_L_geo=100*(0.015*Strsum/Th-0.5*lw);
del_L_const=100*(0.015*Strsum/J);
diff = del_L_geo-del_L_const;
sol=vpasolve(vpa(diff,10)==0);
d1=vpa((100*(0.015*Strsum/sol-0.5*lw)),10);
avg_str=vpa(0.01*d1/(0.5*lw))
dpb
dpb on 20 Jun 2021
I don't have Symbolic TB so can't do anything with above -- if there is a system of equations that can be written as
F(x) = 0
for x, where F(x) is a function that returns a vector value.
then fsolve has a chance as illustrated above for the one variable.
It's not at all clear what are doing above without more time trying to decipher the code than have time for, sorry...
If you can outline the system to be solved, someone can probably help with the setup although there are several examples that should lead you through the process if you can do the above... :)

Sign in to comment.

More Answers (0)

Categories

Find more on Function Handles in Help Center and File Exchange

Products


Release

R2014b

Community Treasure Hunt

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

Start Hunting!