MATLAB Answers

0

I am trying to plot a function with iteration of a variable

Asked by Ethan Miller on 11 Nov 2019 at 22:51
Latest activity Answered by David Hill on 11 Nov 2019 at 23:07
%%
a = .0022; %m
d = .05*a; %m
viscosity_p = 1.2; %cP
viscosity_c = 3.5; %cP
dpdz1 = -10; %mmHg
dpdz2 = -1.3; %kPa
t = 0;
for r =0:.01:a
Vc(r) = ((r.^2 - (a-d)^2)/(4*viscosity_c))*dpdz1;
Vp(r) = ((r.^3 - ((a-d)^2)*r)/(4*viscosity_p*d))*dpdz1;
t = t+1;
end
plot(Vc,r)

  0 Comments

Sign in to comment.

2 Answers

Answer by Star Strider
on 11 Nov 2019 at 23:01

Try this:
a = .0022; %m
d = .05*a; %m
viscosity_p = 1.2; %cP
viscosity_c = 3.5; %cP
dpdz1 = -10; %mmHg
dpdz2 = -1.3; %kPa
t = 0;
rv = linspace(0,a);
for k = 1:numel(rv)
r = rv(k);
Vc(k) = ((r.^2 - (a-d)^2)/(4*viscosity_c))*dpdz1;
Vp(k) = ((r.^3 - ((a-d)^2)*r)/(4*viscosity_p*d))*dpdz1;
t = t+1;
end
plot(Vc,rv)
In MATLAB, indices must be integers greater than 0, so using ‘r’ as an index will not work Also, since ‘a’ is only slightly greater than the 0.01 increment, the colon-described ‘r’ vector contains only one value. The linspace call creates 100 elements in ‘rv’ by default, and as many as you want if you supply a third argument to it.

  0 Comments

Sign in to comment.


Answer by David Hill on 11 Nov 2019 at 23:07

Not sure what t is, but you do not need a for-loop.
r = 0:.01:a;
Vc = ((r.^2 - (a-d)^2)/(4*viscosity_c))*dpdz1;
plot(Vc,r);

  0 Comments

Sign in to comment.