3D plot of function with if statements

조회 수: 1 (최근 30일)
Morten Hansen
Morten Hansen 2019년 8월 4일
댓글: Morten Hansen 2019년 8월 4일
I'm having difficulties with plotting the function below.
Formel.PNG
Note: : and
The correct plot for is depicted below
MC1.PNG
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.

채택된 답변

darova
darova 2019년 8월 4일
THe dot is forgotten
2Capture.PNG
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
  댓글 수: 1
Morten Hansen
Morten Hansen 2019년 8월 4일
Thanks! - I see your point regarding 'cond'. I will implement this going forward

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by