How can i give multiple input values in this function?

10 ビュー (過去 30 日間)
Mirza Farrukh Baig
Mirza Farrukh Baig 2018 年 12 月 7 日
コメント済み: madhan ravi 2018 年 12 月 7 日
I have defined my function and I want to solve for P for multiple phi but it does not give me for different input values it soled for only one value.
I also tried to plot it but it does not give me any plot. Can anyone see my code and guide me it will be very helpfull for me. Thanks in advance.
Inuput values are (pi/6 , pi/3, pi/2, 2pi/3, 5pi/6, pi, 7pi/6, 4pi/3, 3pi/2, 5pi/3, 11pi/6).
function [P] = myfile(phi)
phi = [pi/6 pi/3 pi/2];
for i=1:length(phi)
A = 6*(0.8)*sin(phi);
B = 2+(0.8)*cos(phi);
C = 2+(0.8)^2;
D = (1+0.8*cos(phi)).^2;
P = (A.*B)/(C*D);
end
figure,plot(phi,P)
xlabel('Domain'),ylabel('Pressure'),
title('non-dimensional pressure'),
end

採用された回答

Image Analyst
Image Analyst 2018 年 12 月 7 日
You did not include an index for P, so you just overwrite the same P every iteration. You should have done:
P(i) = (A.*B)/(C*D);
However, you can vectorize the code like this:
phi = [pi/6 , pi/3, pi/2, 2*pi/3, 5*pi/6, pi, 7*pi/6, 4*pi/3, 3*pi/2, 5*pi/3, 11*pi/6]
P = myfile(phi) % Call the myfile function
function P = myfile(phi)
A = 6*(0.8).*sin(phi);
B = 2+(0.8).*cos(phi);
C = 2+(0.8).^2 * ones(1, length(phi));
D = (1+0.8.*cos(phi)).^2;
P = (A.*B)./(C.*D);
figure
plot(phi,A, '*-', 'LineWidth', 2);
hold on;
plot(phi,B, '*-', 'LineWidth', 2);
plot(phi,C, '*-', 'LineWidth', 2);
plot(phi,D, '*-', 'LineWidth', 2);
plot(phi,P, '*-', 'LineWidth', 4);
xlabel('Domain');
ylabel('Pressure');
title('non-dimensional pressure');
legend('A', 'B', 'C', 'D', 'P');
grid on;
end
0001 Screenshot.png
I'm showing the individual A, B, C, and D, just for interest. You just have to change the definition of C slightly. Vectorization is something you should learn as it's one of the most powerful features of modern languages.
  1 件のコメント
Mirza Farrukh Baig
Mirza Farrukh Baig 2018 年 12 月 7 日
Thanks for your explanation.

サインインしてコメントする。

その他の回答 (1 件)

madhan ravi
madhan ravi 2018 年 12 月 7 日
編集済み: madhan ravi 2018 年 12 月 7 日
EDITED
Loop can be avoided:
Phi=pi/6:pi/6:11*pi/6;
P = myfunc(phi); % function call
function P = myfunc(phi) % function definition
A = 6*(0.8).*sin(phi);
B = 2+(0.8).*cos(phi);
C = 2+(0.8).^2;
D = (1+0.8.*cos(phi)).^2;
P = (A.*B)./(C.*D);
figure,plot(phi,P)
xlabel('Domain'),ylabel('Pressure'),
title('non-dimensional pressure'),
end
  6 件のコメント
Mirza Farrukh Baig
Mirza Farrukh Baig 2018 年 12 月 7 日
I used this but did not get the smooth plot.
phi = pi/6:pi/6:11*pi/6;
P = new(phi); % function call
eccentricity = 0.8;
function P = new(phi) % function definition
A = 6*(0.8).*sin(phi);
B = 2+(0.8).*cos(phi);
C = 2+(0.8).^2;
D = (1+0.8.*cos(phi)).^2;
P = (A.*B)./(C.*D);
figure
xq = 0:pi/6:2*pi;
w = interp1(phi,P,xq,'spline');
plot(xq,w)
xlabel('Domain'),ylabel('Pressure'),
title('non-dimensional pressure'),
end
madhan ravi
madhan ravi 2018 年 12 月 7 日
xq = linspace(0,2*pi,1000);

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeExponents and Logarithms についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by