Trouble doing loops for specific values

Hi. I have problem with loops.
I want to calculate for specific values of P which is 0.5, 0.64, 0.7, 1.0, 6.7. How do I do that?
As from below, this code will let P be 0.5, 0.6, 0.7, 0.8, 0.9. But I dont want it to be like that. I want it as specific values like I mentioned just now.
P=0.5;
for i=2:5
P = Pr+0.1;
sol = bvp4c(@(x,y)VK(x,y,n),@VKbc,sol);
fprintf('\n');
fprintf('n \t-Winfinity \n');
plot(sol.x,-sol.y(3,:),lines{i});
end
Thank you in advance!

回答(1 个)

Apparently, this is part of a larger script.
I would define ‘P’ in your loop by assigning it as a vector and then referring to individual elements of the vector:
Pv = [0.5, 0.64, 0.7, 1.0, 6.7];
for i=1:length(Pv)
P = Pv(i);
sol = bvp4c(@(x,y)VK(x,y,n),@VKbc,sol);
fprintf('\n');
fprintf('n \t-Winfinity \n');
plot(sol.x,-sol.y(3,:),lines{i});
end
I can’t run this so I can’t check it. Make appropriate changes so that it works in your code.

2 个评论

Im sorry to trouble you again, but I have tried and im getting confused, should I put Pr or P (at the sol=bvp4c... and everything involving Pr).
function ScriptA
n=0.6;
Pr = [0.5, 0.64, 0.7, 1.0, 6.7];
infinity=20;
solinit = bvpinit(linspace(0,infinity,30),[0 0 0 -1 0 -1 0]);
sol = bvp4c(@(x,y)VK(x,y,n,Pr),@VKbc,solinit);
x = [0:2:20];
y = deval(sol,x);
figure
plot(sol.x,-sol.y(3,:));
drawnow
hold on
for i=1:length(Pv)
Pr = Pv(i);
sol = bvp4c(@(x,y)VK(x,y,n,Pr),@VKbc,sol);
lines = {'b-','c-','r-','g-','k-'};
plot(sol.x,-sol.y(3,:),lines{i});
end
legend('Pr = 0.5','Pr = 0.64','Pr = 0.7','Pr = 1.0','Pr = 6.7',1);
hold off
function yprime = VK(x,y,n,Pr)
a = ((1-n)/(n+1))*x;
c = (y(4)^2+y(5)^2)^((1-n)/(n+1));
yprime = [ c*y(4)
c*y(5)
-2*y(1) - a*c*y(4)
y(1)^2 - (y(2)+1)^2 + (y(3)+a*y(1))*c*y(4)
2*y(1)*(y(2)+1) + (y(3)+a*y(1))*c*y(5)
c*y(7)
Pr*((y(3)+a*y(1))*c*y(7))
];
end
function res = VKbc(ya,yb)
res = [ya(1)
ya(2)
ya(3)
ya(6)+1
yb(1)
yb(2)+1
yb(6)
];
end
end
I don’t understand what you’re doing.
My idea was to replace this line:
Pr = [0.5, 0.64, 0.7, 1.0, 6.7];
with:
Pv = [0.5, 0.64, 0.7, 1.0, 6.7];
Your loop then does what I intend for it to do.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Programming 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by