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

1 view (last 30 days)
%%
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)

Answers (2)

Star Strider
Star Strider on 11 Nov 2019
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.

David Hill
David Hill on 11 Nov 2019
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);

Community Treasure Hunt

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

Start Hunting!