How do I get directional profile of 2D functions ?

3 次查看(过去 30 天)
Hi all,
I have a simple question : Let us define the function z = sqrt(x^2 + y^2) if r < 0.9 and r=0 else. I would like to plot the profile along a line defined by a polar angle A, see the figure within the code or enclosed :
[x,y] = meshgrid(-1:0.1:1);
z = sqrt(x.^2 + y.^2); % or it can be z = x+y or any f(x,y)...
z(sqrt(x.^2 + y.^2) > 0.9)=0; % set the area where z is defined
ax = axes;
h = imagesc(ax,-1:0.1:1,-1:0.1:1,z);
set(ax,'YDir','normal');
% The function is plotted
% Now we plot the line where I want to get the z-profile
A = 30; % in deg
x0 = cos(deg2rad(A));
y0 = sin(deg2rad(A));
hold on
plot(ax,[-x0 0 x0],[-y0 0 y0],'k-');
%end of code
I would like to get the profile along the black line direction. I have played with mesh, surf, etc.. but it does not give the result I want. But I am quite new at MATLAB...
Any help is appreciated :) B

采纳的回答

Star Strider
Star Strider 2017-7-31
One approach:
v = -1:0.1:1;
xline = x0*v;
yline = y0*v;
zline = sqrt(xline.^2 + yline.^2);
zline(zline > 0.9)=0;
figure(2)
plot3(xline, yline, zline)
grid on
  2 个评论
Bastien Rouzé
Bastien Rouzé 2017-7-31
Yeah I agree. But in the example, I created z. Let us imagine that we have imported the z-values matrix without any knowledge on the function z = f(x,y).
Star Strider
Star Strider 2017-7-31
Then let us use the interp2 (link) function to get the values of the plotted surface corresponding to ‘xline’ and ‘yline’:
v = -1:0.1:1;
xline = x0*v;
yline = y0*v;
zline = interp2(x,y,z, xline, yline, 'linear');
figure(3)
plot3(xline, yline, zline)
grid on
This is actually easier. Note that the interpolation vectors (or matrices) must be the same size. It might also be necessary to provide an extrapolation value.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Grid Lines, Tick Values, and Labels 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by