For loop and plotting trouble

1 visualización (últimos 30 días)
Mike
Mike el 27 de Nov. de 2011
Respondida: Nur Fabien Idrissa el 5 de Dic. de 2018
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.

Respuestas (4)

Image Analyst
Image Analyst el 27 de Nov. de 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
Mike el 27 de Nov. de 2011
Thanks for the help. I updated my code, but now I'm running into more trouble. My output vectors for S and Su are empty...Which may be why my plot is now not even loading.
Here's what I have now:
for m=10^-15:10^-1:10^-19;
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;
Su=100*(S-1);
end
semilogx(0.1:100, Su);
ylim([-1.5 2.5])
legend('1','2','3','4','5')
xlabel('Droplet Radius (µm)')
ylabel('Supersaturation (percent)')
title('Kohler Curves for Solution Droplets Containing (NH4)2SO4')
end
  4 comentarios
Mike
Mike el 27 de Nov. de 2011
After making the changes, I'm not even getting the S and Su matrices to output now, and yet I'm not getting any errors.
Image Analyst
Image Analyst el 27 de Nov. de 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?

Iniciar sesión para comentar.


Mike
Mike el 27 de Nov. de 2011
% Define values and constants
s=75.64*10^-3; % Sigma (Interfacial surface energy) [J m^-2]
n=3.348*10^28; % # H2O molecules per unit volume of liquid [molecules/m^3]
k=1.38*10^-23; % Boltzmann constant [J K^-1 kg^-1]
Ms=0.13214; % Molecular weight of (NH4)2SO4 [kg/mol]
Mw=0.018; % Molecular weight of H2O [kg/mol}
T=293; % Temperature [K]
p=1769; % Density of (NH4)2SO4 [kg m^-3]
a=3; % Number of dissociations of (NH4)2SO4
The rest of the code is what I have above, just with Su changed to Su(sIndex). When I run the entire code, the vectors S and Su do not appear in the workspace. If I just run the inner loop, I get the following error:
In an assignment A(I) = B, the number of elements in B and
I must be the same.

Nur Fabien Idrissa
Nur Fabien Idrissa el 5 de Dic. de 2018
hello Mike!
how did you solve the question. may you share with me the process.
thanks!

Categorías

Más información sobre Programming en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by