Surface plot for two variable piecewise function

I need help plotting the following piecewise function in Matlab as surface plot. Any help is appreciated!!
x1 and x2 are [0,1]

3 件のコメント

Torsten
Torsten 2023 年 10 月 21 日
編集済み: Torsten 2023 年 10 月 21 日
Are x1, x2 assumed to be >= 0 ?
Briana Canet
Briana Canet 2023 年 10 月 21 日
x1 and x2 are from 0 to 1.
Torsten
Torsten 2023 年 10 月 21 日
Then you can follow @Dyuman Joshi 's hints.

サインインしてコメントする。

回答 (1 件)

Dyuman Joshi
Dyuman Joshi 2023 年 10 月 21 日

0 投票

Utilize meshgrid to generate the points and evaluate the function using the points.
Then employ surf to plot the piecewise function.

9 件のコメント

Briana Canet
Briana Canet 2023 年 10 月 21 日
移動済み: Dyuman Joshi 2023 年 10 月 21 日
I used the following code:
Can someone verify this is correct?
clc;
clear
U = @(x1,x2) (1-exp(-5*(x1.^3+x2.^2).^1/3)) .* (((x1.^3 + x2.^2) >= 0) & ((x1.^3 + x2.^2) <= 1)) + (x1.^3 + x2.^2 - 1) .* (((x1.^3 + x2.^2) >= 1) & ((x1.^3 + 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)
Torsten
Torsten 2023 年 10 月 21 日
編集済み: Torsten 2023 年 10 月 21 日
Why do you plot for -2 <= x1, x2 <= 3 if 0 <= x1, x2 <= 1 as you stated above?
And this part of your code seems superfluous:
x = linspace(-1, 3);
y = linspace(-1, 3);
[X,Y] = ndgrid(x,y);
Briana Canet
Briana Canet 2023 年 10 月 21 日
Yeah I just fixed that.
Briana Canet
Briana Canet 2023 年 10 月 21 日
編集済み: Briana Canet 2023 年 10 月 21 日
There is a clear discontinuity when I plot though. How can I tell by how much I must adjust the equations to fix that?
clc;
clear
U = @(x1,x2) (1-exp(-5*(x1.^3+x2.^2).^1/3)) .* (((x1.^3 + x2.^2) >= 0) & ((x1.^3 + x2.^2) <= 1)) + (x1.^3 + x2.^2 - 1) .* (((x1.^3 + x2.^2) >= 1) & ((x1.^3 + x2.^2) <= 2));
x = linspace(-1, 3);
y = linspace(-1, 3);
[X,Y] = ndgrid(x,y);
figure
fsurf(U, [ -0 1 -0 1], 'MeshDensity',75)
Dyuman Joshi
Dyuman Joshi 2023 年 10 月 21 日
編集済み: Dyuman Joshi 2023 年 10 月 21 日
You don't need to generate the grid of points with fsurf.
There is a discontinuity because that's how the function works.
U = @(x1,x2) (1-exp(-5*(x1.^3+x2.^2).^1/3)) .* (((x1.^3 + x2.^2) >= 0) & ((x1.^3 + x2.^2) <= 1)) + (x1.^3 + x2.^2 - 1) .* (((x1.^3 + x2.^2) >= 1) & ((x1.^3 + x2.^2) <= 2));
%The domain of x1 and x2 is [0 1]
fsurf(U, [0 1 0 1], 'MeshDensity', 75)
view([45 45])
Torsten
Torsten 2023 年 10 月 21 日
編集済み: Torsten 2023 年 10 月 21 日
There is a clear discontinuity when I plot though. How can I tell by how much I must adjust the equations to fix that?
Compute the values for your two function definitions for x1^3+x2^2 = 1 and make these values equal by adding appropriate values to your two functions. There is not a single solution, but many ways to do so. But I don't know what function manipulations are allowed in your case.
Briana Canet
Briana Canet 2023 年 10 月 21 日
I did:
U(1,0) = U(1,0)
0=1-e^-5
I then subtracted that 1-e^(-5) from the first equation:
clc;
clear
U = @(x1,x2) (1-exp(-5*(x1.^3+x2.^2).^1/3)-(1-exp(-5))) .* (((x1.^3 + x2.^2) >= 0) & ((x1.^3 + x2.^2) <= 1)) + (x1.^3 + x2.^2 - 1) .* (((x1.^3 + x2.^2) >= 1) & ((x1.^3 + x2.^2) <= 2));
figure
fsurf(U, [ 0 1 0 1], 'MeshDensity',75)
Torsten
Torsten 2023 年 10 月 21 日
編集済み: Torsten 2023 年 10 月 21 日
A bracket around 1/3 was missing. Should be
U = @(x1,x2) (1-exp(-5*(x1.^3+x2.^2).^(1/3))-(1-exp(-5))) .* (((x1.^3 + x2.^2) >= 0) & ((x1.^3 + x2.^2) <= 1)) + (x1.^3 + x2.^2 - 1) .* (((x1.^3 + x2.^2) >= 1) & ((x1.^3 + x2.^2) <= 2));
figure
fsurf(U, [ 0 1 0 1], 'MeshDensity',75)
Dyuman Joshi
Dyuman Joshi 2023 年 10 月 21 日
"A bracket around 1/3 was missing."
Ah, that should be it.

サインインしてコメントする。

カテゴリ

質問済み:

2023 年 10 月 21 日

コメント済み:

2023 年 10 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by