How can I plot the results for a for loop?

1 view (last 30 days)
Hi,
I'm very new to matlab and I can't figure this out!
I need to write a for loop changing a single variable. I need to plot the results for each variable value. This is what I have so far
u=2.5
o=0.15
d=0:0.1:10
PSD=(1/(o*((2*pi)^0.5)))*exp(-((d-u).^2)/(2*o^2))
kc=0.865
for i=1:3
dc(i)=i
Td=1-exp(-kc*(d./dc(i)).^2)
Solremoval=(sum(Td.*PSD)/sum(PSD))*100
plot(Solremoval,dc)
end
Please help!

Accepted Answer

Stephen23
Stephen23 on 13 Apr 2015
Edited: Stephen23 on 14 Apr 2015
You should not use the variable names i and j as these are both names of the inbuilt imaginary unit. You might also like to place a semicolon after each line to prevent them printing to the command window.
Although figuring out non-functioning code is a little bit of a guessing game, this seems to prduce a similar result to what you are trying to do, via the very handy helper-function bsxfun and without any loops at all:
u = 2.5;
o = 0.15;
d = (0:0.1:10).';
PSD = (1/(o*((2*pi)^0.5)))*exp(-((d-u).^2)/(2*o^2));
kc = 0.865;
dc = 1:3;
Td = 1 - exp(-kc*bsxfun(@rdivide,d,dc).^2);
Solremoval = 100*sum(bsxfun(@times,Td,PSD))/sum(PSD);
plot(Solremoval,dc,'*-')
This produces the following figure:

More Answers (0)

Categories

Find more on Function Creation 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!