Mesh/Surf plot of function with if statements
显示 更早的评论
I’m trying to plot the following function (cost curve), basically combining two functions in a plot separated by an upper bound:

However, I can’t get the if statements working properly – the functions are not combining correctly in the plot.
It’s my code so far:
[X,Y] = meshgrid(0:1:20); %Evaluates the function between 0 and 20 in both x and y. Interval set at 1.
if 4*X.^2+3*Y.^2<=225
Z = 2*sqrt(100*X.^2+75*Y.^2); %Conditionally Function (1)
else
Z = 75+4/3*X.^2+Y.^2; %Conditionally Function (2)
end
figure(1); %Allows working with multiple figures simultaneously
mesh(X,Y,Z) %mesh plot
colorbar %add colorbar in plot
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('Cost, C(q;P)','FontSize',8,'FontWeight','normal','Color','k') %label z-axis
figure(2); %Next figure
surf(X,Y,Z) %surface plot
colorbar %add colorbar in plot
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('Cost, C(q;P)','FontSize',8,'FontWeight','normal','Color','k') %label z-axis
I have also looked at conditionally defined expression or function using piecewise but couldn’t get it to work either.
Please could someone offer corrected code or suggestions for changes?
Many thanks in advance.
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Line Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!