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

1 回表示 (過去 30 日間)
Ethan Miller
Ethan Miller 2019 年 11 月 11 日
回答済み: David Hill 2019 年 11 月 11 日
%%
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)

回答 (2 件)

Star Strider
Star Strider 2019 年 11 月 11 日
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 2019 年 11 月 11 日
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);

カテゴリ

Help Center および File ExchangeCalculus についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by