Good evening, for my thesis I should make a 3d plot in which I want something similar to this:
I have a function f(x,y)=x^2-2*x+1+y^2 subject to constraints g(x,y)=x^4+y^4-1.
I'm interested only in the plot.

 採用された回答

Matt J
Matt J 2022 年 4 月 11 日
編集済み: Matt J 2022 年 4 月 11 日

1 投票

Use this,
[X,Y]=ndgrid(linspace(-2,2,500));
G=X.^4+Y.^4;
cm = contourf(X,Y,G,1); close
[~, cA] = getContourLineCoordinates(cm);
f=@(x,y)x.^2-2*x+1+y.^2;
fsurf(f,[-4,4],'FaceColor','b','FaceAlpha',0.3,'EdgeColor','none'); hold on
for i=1:numel(cA)
x=cA{i}(:,1);
y=cA{i}(:,2);
z=f(x,y);
line(x,y,z,'COlor','r','LineWidth',2)
end
hold off
xlabel X; ylabel Y; view(150,-2)

3 件のコメント

Michele Guaragno
Michele Guaragno 2022 年 4 月 11 日
it was exactly what I wanted. But I get this error:
Unrecognized function or variable 'getContourLineCoordinates'.
Matt J
Matt J 2022 年 4 月 12 日
編集済み: Matt J 2022 年 4 月 12 日
Because you didn't download it from the link I gave you.
Michele Guaragno
Michele Guaragno 2022 年 4 月 12 日
Ah ok, I'm new in Matlab, thanks a lot.

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

その他の回答 (1 件)

Torsten
Torsten 2022 年 4 月 11 日

2 投票

x=-2:0.01:2;
y=-2:0.01:2;
[X,Y] = meshgrid(x,y);
Z = X.^2-2*X+1+Y.^2;
xx=-1:0.01:1;
yy=-1:0.01:1;
y1 = (1-xx.^4).^(0.25);
y2 = -(1-xx.^4).^(0.25);
z1 = xx.^2-2*xx+1+y1.^2;
z2 = xx.^2-2*xx+1+y2.^2;
surf(X,Y,Z)
hold on
plot3(xx,y1,z1,'Linewidth',4,'Color','red')
hold on
plot3(xx,y2,z2,'Linewidth',4,'Color','red')
view([2,2,4])

1 件のコメント

Michele Guaragno
Michele Guaragno 2022 年 4 月 12 日
Thanks a lot, I really appreciate your help.

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

カテゴリ

製品

リリース

R2021a

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by