Adding constraints to function plots in 2-D and 3-D

13 views (last 30 days)
Hi,
I am new to Matlab.
I have the code snippet below.
Assume that I want to skip parts of the domain of f, i.e. (x,y), where x^2 + y^2 = c (c is any constant value in my code).
So, I want the mesh function to only plot functional values of the points in the domain of my function, which satisfify the above constraint.
How do I go about editing the code snippet below in order to incorporate the type of constraint defined above?
You timely assistance would be appreciated.
f = @ (x , y ) x .* sin( x .* y );
[X , Y ] = meshgrid (0:.1:5 , pi :.01* pi :2* pi );
Z = f (X , Y );
mesh (X ,Y , Z )
  1 Comment
Wan Ji
Wan Ji on 27 Aug 2021
I feel a litlle bit confused of your question, which domain should be skipped?
x^2+y^2<c
or
x^2+y^2>c

Sign in to comment.

Answers (1)

Wan Ji
Wan Ji on 29 Aug 2021
Hi,
I have translated the Cartesian to polar system so as to satisfy your requirement
f = @ (x , y ) x .* sin( x .* y );
minR = sqrt(0+pi^2);
maxR = sqrt(5^2+(2*pi)^2);
minTheta = atan2(pi,5);
maxTheta = atan2(2*pi,0);
r = linspace(minR, maxR, 101);
theta = linspace(minTheta, maxTheta, 101);
[R, T] = meshgrid(r, theta);
X = R.*cos(T);
Y = R.*sin(T);
% [X , Y ] = meshgrid (0:.1:5 , pi :.01* pi :2* pi );
Z = f (X , Y );
Z(~(X<=5 & X>=0 & Y<=2*pi & Y>=pi)) = NaN;
mesh (X ,Y , Z )

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!