I would like to plot
f(x,y) = x^2*y/(x^2 + y^2)
How do I do this in matlab?

댓글 수: 2

sally_wu
sally_wu 2015년 10월 3일
편집: sally_wu 2015년 10월 3일
Maybe like this?
x=2;
y=3;
f(x,y) = x^2*y/(x^2 + y^2);
linspace(x,y)
Anthont Goddard
Anthont Goddard 2015년 10월 3일
I am just getting a load of text output. Not a 3d plot

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

 채택된 답변

Star Strider
Star Strider 2015년 10월 3일

1 개 추천

You need to use the meshgrid (or equivalently, ndgrid) function:
x = linspace(-1, 1);
y = linspace(-2, 2);
[X,Y] = meshgrid(x,y);
f = @(x,y) x.^2.*y./(x.^2 + y.^2);
figure(1)
surf(X,Y,f(X,Y))
grid on
I created a function for ‘f’ here, but you could as easily use it simply as:
f = X.^2.*Y./(X.^2 + Y.^2);
and then plot as:
plot(X,Y,f)
I ‘vectorised’ your code to do element-wise operations. See the documentation on Array vs. Matrix Operations for a full discussion.

댓글 수: 4

Anthont Goddard
Anthont Goddard 2015년 10월 3일
When I run that script I get an error. Subscript indices must either be real positive integers or logicals.
This code:
x = linspace(-1, 1);
y = linspace(-2, 2);
[X,Y] = meshgrid(x,y);
f = @(x,y) x.^2.*y./(x.^2 + y.^2);
figure(1)
surf(X,Y,f(X,Y))
grid on
ran for me without error, and produced this plot:
Anthont Goddard
Anthont Goddard 2015년 10월 3일
Ah that worked. Thanks :)
Star Strider
Star Strider 2015년 10월 3일
My pleasure!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

질문:

2015년 10월 3일

댓글:

2015년 10월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by