Tips on combining while loop

조회 수: 5 (최근 30일)
Fahrizal Perdana Fahmul
Fahrizal Perdana Fahmul 2021년 4월 15일
답변: Rik 2021년 4월 15일
Hello forum,
I need some tips on combining 3 same while loop with different input. I separated the input, but actually the input can be united into one.
V = -12
V1 = 12
V = 0
can be combined into V=[-12 0 12]
But unfortunately I do not know how.
Thank you.
%Pohlhausen/thwaites polynomial
clear;clc;close;
i = 1;
j = 1;
k = 1;
V = -12; %Upper case lambda
V1 = 12; %Upper case lambda
V2 = 0; %Upper case lambda
ys = 0; %y/delta - normalized y axis. delta is BL thickness (minimum)
ys1 = 0; %y/delta - normalized y axis. delta is BL thickness (minimum)
ys2 = 0; %y/delta - normalized y axis. delta is BL thickness (minimum)
%upper case lambda -12
while ys <=1 ; %y/delta - normalized y axis. delta is BL thickness (maximum)
x(i) = ys; %range of normalize y
f (i)=(2*ys - 2*ys^3 + ys^4) + ((V/6*ys)*(1-ys)^3); %u/U_infinity - pohlhausen velocity distribution
ys = ys + 0.01; %step of normalize y
i = i+1; %array step
end
%upper case lambda -12
while ys1 <=1 ; %y/delta - normalized y axis. delta is BL thickness (maximum)
xa(j) = ys1; %range of normalize y
g (j)=(2*ys1 - 2*ys1^3 + ys1^4) + ((V1/6*ys1)*(1-ys1)^3); %u/U_infinity - pohlhausen velocity distribution
ys1 = ys1 + 0.01; %step of normalize y
j = j+1; %array step
end
%upper case lambda 0
while ys2 <=1 ; %y/delta - normalized y axis. delta is BL thickness (maximum)
xb(k) = ys2; %range of normalize y
h (k)=(2*ys2 - 2*ys2^3 + ys2^4) + ((V2/6*ys2)*(1-ys2)^3); %u/U_infinity - pohlhausen velocity distribution
ys2 = ys2 + 0.01; %step of normalize y
k = k+1; %array step
end
figure(1)
plot(x,f,'r-',xa,g,'b-',xb,h,'c-','LineWidth', 2)
hold on
grid on
title('Pohlhausen Velocity distribution')
ylabel('u/U_\infty', 'FontSize', 20)
xlabel('y* = y/\delta','FontSize', 20)
legend('\Lambda = 12','\Lambda = -12','\Lambda = 0','location','southeast')
ylim([0 1.1])

답변 (1개)

Rik
Rik 2021년 4월 15일
You were creating a function, why not make that explicit?
%Pohlhausen/thwaites polynomial
%clear;clc;close;
% ^^^^^^ you don't need this
i = 1;
j = 1;
k = 1;
V = -12; %Upper case lambda
V1 = 12; %Upper case lambda
V2 = 0; %Upper case lambda
ys = 0; %y/delta - normalized y axis. delta is BL thickness (minimum)
ys1 = 0; %y/delta - normalized y axis. delta is BL thickness (minimum)
ys2 = 0; %y/delta - normalized y axis. delta is BL thickness (minimum)
%upper case lambda -12
[x,f]=myFun(i,V,ys);
%upper case lambda -12
% ^ is that correct?
[xa,g]=myFun(j,V1,ys1);
%upper case lambda 0
[xb,h]=myFun(k,V2,ys2);
figure(1),clf(1)
% ^^^^^^^
%during debugging you can add this to make sure your figure is clean
plot(x,f,'r-',xa,g,'b-',xb,h,'c-','LineWidth', 2)
%hold on
% you aren't plotting anything else, so hold on is not required
grid on
title('Pohlhausen Velocity distribution')
ylabel('u/U_\infty', 'FontSize', 20)
xlabel('y* = y/\delta','FontSize', 20)
legend('\Lambda = 12','\Lambda = -12','\Lambda = 0','location','southeast')
ylim([0 1.1])
function [x,f]=myFun(i,V,ys)
%Write documentation explaining this function here
%
%write syntax examples as well
%Do input checking and make sure any errors that can occur will trigger an
%error here, so the user can understand what they need to change.
%After this input validation step, no error is allowed to occur.
x=zeros(1,ceil((ys-1)/0.01);%pre-allocate array
f=zeros(size(x));%pre-allocate array
while ys <=1 ; %y/delta - normalized y axis. delta is BL thickness (maximum)
x(i) = ys; %range of normalize y
f(i)=(2*ys - 2*ys^3 + ys^4) + ...
((V/6*ys)*(1-ys)^3); %u/U_infinity - pohlhausen velocity distribution
ys = ys + 0.01; %step of normalize y
i = i+1; %array step
end
if numel(x)>i
%remove padding
x(i+1:end)=[];
f(i+1:end)=[];
end
end

카테고리

Help CenterFile Exchange에서 Data Types에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by