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 CenterFile Exchange에서 Utilities for the Solver에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by