
3D plot of function with if statements
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Morten Hansen
am 4 Aug. 2019
Kommentiert: Morten Hansen
am 4 Aug. 2019
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 Kommentare
Akzeptierte Antwort
darova
am 4 Aug. 2019
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
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Surface and Mesh Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!