For loop and plotting trouble
Show older comments
I need to plot the results of a physics function called the Kohler equation... I tried setting up a loop for a given range of the variable r as such:
m=10^-15;
for r=0.1:100;
S(r)=(exp((2*s)/(n*k*T*r))*((1+((a*m*Mw)/(Ms*(4/3)*pi*(r^3))*p-m)))^-1);
end
SS(S)=100*(S-1);
semilogr (SS)
xlim([0.1 100])
ylim([-1.5 0.5])
> So, the first issue I run into is an error in the function itself:
Attempted to access (0.1); index must be a positive integer or logical.
I seem to remember that there's a simple fix for this, like adding a period somewhere, but I'm not sure where.
> The second issue is that I need to plot this for 5 different values of m: 10^-15, 10^-16, 10^-17, 10^-18, and 10^-19. Would it be practical to set up an additional loop for this? The intervals are different for each value, so I wasn't sure how to do this.
> And then the final issue is that I need to plot r on the x-axis, scaled logarithmically, vs. SS on the y axis, with a linear scale. I'm not sure if I have that set up right...
I'm relatively inexperienced (and also out of practice) with Matlab, so I apologize if some of my questions seem elementary.
Answers (4)
Image Analyst
on 27 Nov 2011
If you want to iterate over r, then you'll need a counter as the index for S, like this
sIndex = 1;
for r=0.1:100;
S(sIndex )=(exp((2*s)/(n*k*T*r))*((1+((a*m*Mw)/(Ms*(4/3)*pi*(r^3))*p-m)))^-1);
sIndex = sIndex + 1;
end
For the second issue, you could have a second, outer loop to loop over the various values of m. You'd want to plot S before the "end" of this outer m loop, unless you want to make S a 2D array.
For the third issue, you can plot r vs. S and it should look right.
plot(0.1:100, S, 'b-');
Mike
on 27 Nov 2011
4 Comments
Image Analyst
on 27 Nov 2011
Please edit the code to supply typical values for n, k, T, etc. - whatever we need to make this code snippet run.
Su should be a single scalar value since you didn't make it an array like you did for S. Use Su(sIndex) if you want it to be an array.
Mike
on 27 Nov 2011
Mike
on 27 Nov 2011
Image Analyst
on 27 Nov 2011
So you don't want to make it easy for us to help you by posting typical values for n, k, T, etc. like I asked? Strange. It would be quicker if you helped.
Define what you mean by "output" - does that mean plot, like your call to semilogx() doesn't do anything whatsoever??? If you set a breakpoint right after the loop, are S and Su there?
Mike
on 27 Nov 2011
Nur Fabien Idrissa
on 5 Dec 2018
0 votes
hello Mike!
how did you solve the question. may you share with me the process.
thanks!
Categories
Find more on Programming 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!