How to return the lowest value of a for loop?

5 views (last 30 days)
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

Accepted Answer

Star Strider
Star Strider on 2 Oct 2014
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

More Answers (0)

Community Treasure Hunt

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

Start Hunting!