Using fzero to keep tolerance within limits
8 次查看(过去 30 天)
显示 更早的评论
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);
0 个评论
采纳的回答
dpb
2021-6-19
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 个评论
dpb
2021-6-20
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... :)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Function Handles 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!