
3D plot of function with if statements
1 回表示 (過去 30 日間)
古いコメントを表示
I'm having difficulties with plotting the function below.

Note:
:
and 
The correct plot for
is depicted below

Code:
%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%FIGURE 3.2%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%
P1 = 20
P2 = 15
P3 = 5
Z3 = 15
[X,Y] = meshgrid(0:0.5:10); %Evaluates the function between 0 and 10 in both X and Y. Interval set at 0.5.
MC1_1=(2*P3*P1*X/(sqrt(P3*(P1*X.^2+P2*Y.^2)))).*(sqrt((1/P3)*(P1*X.^2+P2*Y.^2))<=Z3); %Conditionally Function (1)
MC1_2=(2*P1*X/Z3).*(sqrt((1/P3)*(P1*X.^2+P2*Y.^2))>Z3); %Conditionally Function (2)
MC1 = MC1_1 + MC1_2;
figure(1); %Allows working with multiple figures simultaneously
mesh(Y,X,MC1) %mesh plot
colorbar %add colorbar in plot
title('Figure 3.2. MC_1(q;P) for Example 3.2','FontSize',10,'FontWeight','normal','Color','k') %add figure title
xlabel('Output, q_{2}','FontSize',8,'FontWeight','normal','Color','k') %label x-axis
ylabel('Output, q_{1}','FontSize',8,'FontWeight','normal','Color','k') %label y-axis
zlabel('MC_1(q;P)','FontSize',8,'FontWeight','normal','Color','k') %label z-axis
As far as I can see the problem is with the MC1_1 part of the code based on my plot and data in the variable. I guess the if statements isn't working properly.
Please could someone offer corrected code or suggestions/references for changes?
Many thanks in advance.
0 件のコメント
採用された回答
darova
2019 年 8 月 4 日
THe dot is forgotten

Try to write more readable code:
[X,Y] = meshgrid(0:0.5:10); %Evaluates the function between 0 and 10 in both X and Y. Interval set at 0.5.
cond = sqrt(1/P3*(P1*X.^2+P2*Y.^2));
MC1_1 = 2*P3*P1*X./(cond*P3).*(cond<=Z3); %Conditionally Function (1)
MC1_2 = (2*P1*X/Z3).*(cond>Z3); %Conditionally Function (2)
Mush easier to find a mistake
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Surface and Mesh Plots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!