how do i find the min value using for loop?

clc
clear all
syms x
for yr = linspace(0,1,10)
f = sin(x);
fL = [0 pi];
iL = [0 pi];
Volume = pi*int((f-yr)^2,iL(1),iL(2));
s=double(Volume)
end
h=min(s)

 Accepted Answer

syms x
smin = inf; % initialize smin
for yr = linspace(0,1,10)
f = sin(x);
fL = [0 pi];
iL = [0 pi];
Volume = pi*int((f-yr)^2,iL(1),iL(2));
s=double(Volume);
if s<smin,
smin = s;
end
end
%h=min(s)
smin
smin = 0.9437

8 Comments

I could not understand it why have you choosen infinity as smin? Can you please explain?
You put a very large value as the initial value to ensure that the min is from the data. You can go through a simple example of s = -10, -100, 200, 1000 and examine what happens with:
smin = inf
for s = [-10, -100, 200, 1000]
if s<smin,
smin = s;
end
end
thank you so much.now i could understand it.its a good logic.once again thank you.
and how can i find the point at which it is minimum
clc
clear all
syms x
smin = inf; % initialize smin
for yr = linspace(0,1,10)
f = sin(x);
fL = [0 pi];
iL = [0 pi];
Volume = pi*int((f-yr)^2,iL(1),iL(2));
s=double(Volume);
if s<smin,
smin = s;
end
end
smin
for yr = linspace(0,1,10)
f = sin(x);
fL = [0 pi];
iL = [0 pi];
Volume = pi*int((f-yr)^2,iL(1),iL(2));
h=double(Volume)
if smin==h for yr= linspace(0,1,10);
disp(yr)
end
end
end
i have tried this one but this doesnt make any sense can you help
syms x
smin = inf; % initialize smin
locmin = nan;
for yr = linspace(0,1,10)
f = sin(x);
fL = [0 pi];
iL = [0 pi];
Volume = pi*int((f-yr)^2,iL(1),iL(2));
s=double(Volume);
if s<smin,
smin = s;
locmin = yr;
end
end
smin, yr
smin = 0.9437
yr = 1
ahh it is wrong.its taking yr=1 for max and min too.can you please check it.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!