Filter löschen
Filter löschen

How do I get directional profile of 2D functions ?

3 Ansichten (letzte 30 Tage)
Bastien Rouzé
Bastien Rouzé am 31 Jul. 2017
Kommentiert: Star Strider am 31 Jul. 2017
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

Akzeptierte Antwort

Star Strider
Star Strider am 31 Jul. 2017
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 Kommentare
Bastien Rouzé
Bastien Rouzé am 31 Jul. 2017
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 am 31 Jul. 2017
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by