How to return the lowest value of a for loop?

6 ビュー (過去 30 日間)
Bri
Bri 2014 年 10 月 2 日
回答済み: Star Strider 2014 年 10 月 2 日
This is the problem I have to solve. I pretty much have it solved, I just need help returning a specific value in my for loop.
myminimum(f,a,b) which takes three inputs:
f: A function handle.
a: A real number.
b: A real number.
Note: You may assume that f(x) is a quadratic function and that a < b.
Does: Finds the minimum value of f(x) on [a, b] keeping in mind that this may occur either at the critical point (if the critical point is in [a, b]) or at one of the endpoints. You’ll probably want to use some if statements to check cases.
Returns: The minimum value of f(x) on [a, b].
This is my code:
function m=myminimum (f,a,b);
syms x;
v=subs(f(x),a);
vv=subs(f(x),b);
cv=subs(diff(f(x),0));
for t=(a:b);
m=subs(f(x),t)
end
When I run the code it needs to return the lowest value that is found in the for loop. How do I get matlab to store the values of the for loop and then how do I tell matlab to return the lowest value that it stored?
Thanks

採用された回答

Star Strider
Star Strider 2014 年 10 月 2 日
I would tweak the loop to:
m_min = Inf;
for t=(a:b);
m=subs(f(x),t);
m_min = min(m_min, m);
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeUtilities for the Solver についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by