Piecewise Function Surface Plot
19 views (last 30 days)
Show older comments
Need to plot 2 utility functions on the same surface plot. They should connect seamlessly.
U(x1, x2) = 2*x1 + 2*(x2)^2 [0 <= (x1 + (x2)^2) <= 1]
U(x1, x2) = x1 + (x2)^2 + 2 [1 <= (x1 + (x2)^2) <= 2]
I have no idea how to accomplish this especially with the bounds containing a function, itself
0 Comments
Answers (2)
Torsten
on 25 Oct 2022
Hint: The function can be defined as
fun = @(x,y) (2*x+2*y^2)*(x+y^2>=0 && x+y^2<=1) + (x+y^2+2)*(x+y^2>1 && x+y^2<=2);
0 Comments
Star Strider
on 25 Oct 2022
They are not seamless because the first surface ends with Z=1 and the second begins with Z=2. Change the constant in the second, and they are seamless.
U = @(x1,x2) (2*x1 + 2*x2.^2) .* (((x1 + x2.^2) >= 0) & ((x1 + x2.^2) <= 1)) + (x1 + x2.^2 + 2) .* (((x1 + x2.^2) >= 1) & ((x1 + x2.^2) <= 2));
x = linspace(-1, 3);
y = linspace(-1, 3);
[X,Y] = ndgrid(x,y);
figure
fsurf(U, [ -2 3 -2 3], 'MeshDensity',75)
U = @(x1,x2) (2*x1 + 2*x2.^2) .* (((x1 + x2.^2) >= 0) & ((x1 + x2.^2) <= 1)) + (x1 + x2.^2 + 1) .* (((x1 + x2.^2) >= 1) & ((x1 + x2.^2) <= 2));
x = linspace(-1, 3);
y = linspace(-1, 3);
[X,Y] = ndgrid(x,y);
figure
fsurf(U, [ -2 3 -2 3], 'MeshDensity',75)
.
0 Comments
See Also
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

