MatLab - Euclidean Distance Plot 3D - matlab

Im new in matlab programming and I have small issue.
I want to draw a plot 3d of Euclidean distance function for 2 coordinates, like in this picture below:
Could you help me with the source code? How I can draw this plot?
My first thoughts was wrong:
[A] = meshgrid(-100:.5:100, -100:.5:100);
D1 = bwdist(A);
figure
surf(double(A), double(D1))

It is done like this...
[x, y] = meshgrid(-100:.5:100, -100:.5:100);
The you have to calculated the euclidean distances. I assume you want them with the origin.
z = (x.^2 + y.^2).^0.5; % square root of sum of squares (euclidean distance with origin)
surf(x, y, z);
NOTE: meshgrid(-100:.5:100, -100:.5:100) might make the resolution of the plot too high. If you have trouble viewing the plot, reduce the resolution.
Use [x, y] = meshgrid(-100:5:100, -100:5:100);

Related

How to calculate the volume under two intersection surface?

I like to calculate the volume under the two intersection plane. The two plane is draw use this code.
P1 = [575,0,400];
P2 = [287.5,0,662];
P3 = [575,3500,154];
normal = cross(P1-P2, P1-P3)
syms x y z
P = [x, y, z]
ep1=dot(normal, P-P1)
% get the equation
Z = solve(ep1,z)
% draw the first plane
ezsurf(Z,[287.5,575,0,3500])
hold on
% draw the second horizontal plane
[x,y]=meshgrid(0:500:3500)
z = ones(8,8)*440
surf(x, y, z)
So I must calculate the volume under the first plane.
I used this code, but I don't know how to construct matrix Zm used the the symbols equation Z. And how can I use meshgrid and surf not ezsurf draw the first plane.
%f=#(x,y)(interp2(Zm,Xq,Yq))
% I want to calculate volume under the plane ranged by Xmin=2.875, Xmax=575,Ymin=0,Ymax=3500
%volume = quad2d(f,(287.5),575,0,3500)
%volume = integral2(f,287.5,575,0,3500)
Thanks a lot.
As an alternative strategy that might be easier to understand I would suggest, instead of interpolating, using some geometry formulas to calculate the area. You can break down the 3D shape into a simple triangular prism and an irregular tetrahedron. There are well defined generalized formulas for both of these.
http://mathcentral.uregina.ca/QQ/database/QQ.09.03/peter2.html

How to plot a 3D surface with a circle in it?

I have a rational polynomial function. I find zeros of numerator and denominator of it. Now I want to draw this function and I do it with meshgrid and mesh command in matlab. How can I draw a circle in this shape? I add my result figure at first and second figure is an image that I want to be like that( draw red circle).
Create x and y for your circle:
r = 1;
theta = 0:0.1:2*pi;
x = r*cos(theta);
y = r*sin(theta);
Get the value of your function at the x and y's and plot a line in 3D with the values:
z = f(x,y);
plot3(x,y,z);
The final result may have some artefacts where the line crosses in and out of the surface. If you are not so concerned about the accuracy in the plot add a very small value to z to "lift" it above the surface.

Generate Equation from 3D surf Plot in Matlab

I am wondering if anyone knows (or is it possible?) how to generate a trend equation from a 3D surf plot from Matlab? I understand that we can create trendline for 2D plots (linear and nonlinear) and show its equation, but how about 3D plot? Can we create something like:
z = ax + by?
Regards
Kit
If you have the curve fitting toolbox you can fit 3D surfaces using cftool as described here.
Here's an example:
[X,Y] = meshgrid(1:100,1:100);
X = reshape(X,numel(X),1);
Y = reshape(Y,numel(Y),1);
Z = 3*X+4*Y;
plot3(X,Y,Z)
f = fit([X, Y], Z, 'poly11');
coeffvalues(f)

Why not spherical plot? How to plot 3D-polar-plot in Matlab?

[r,t] = meshgrid(linspace(0,2*pi,361),linspace(0,pi,361));
[x,y]=pol2cart(sin(t)*cos(r),sin(t)*sin(r));
%[x,y]=pol2cart(r,t);
surf(x,y);
I played with this addon but trying to find an default function to for this. How can I do the 3D-polar-plot?
I am trying to help this guy to vizualise different integrals here.
There are several problems in your code:
You are already converting spherical coordinates to cartesian coordinates with the sin(theta)*cos(phi) and sin(theta)*sin(phi) bit. Why are you calling pol2cart on this (moreover, we're not working in polar coordinates!)?
As natan points out, there is no third dimension (i.e. z) in your plot. For unity radius, r can be omitted in the spherical domain, where it is completely defined by theta and phi, but in the cartesian domain, you have all three x, y and z. The formula for z is z = cos(theta) (for unit radius).
You didn't read the documentation for surf, which says:
surf(Z,C) plots the height of Z, a single-valued function defined over a geometrically rectangular grid, and uses matrix C, assumed to be the same size as Z, to color the surface.
In other words, your surf(x,y) line merely plots the matrix x and colors it using y as a colormap.
Here's the above code with the mistakes fixed and plotted correctly:
[f,t] = meshgrid(linspace(0,2*pi,361),linspace(0,pi,361));
x = sin(t)*cos(f);
y = sin(t)*sin(f);
z = cos(t);
surf(x,y,z)

How to plot a second graph instead of color coding in matlab

i just started with my master thesis and i already am in trouble with my capability/understanding of matlab.
The thing is, i have a trajectory on a surface of a planet/moon whatever (a .mat with the time, and the coordinates. Then i have some .mat with time and the measurement at that time.
I am able to plot this as a color coded trajectory (using the measurement and the coordinates) in scatter(). This works awesomely nice.
However my problem is that i need something more sophisticated.
I now need to take the trajectory and instead of color-coding it, i am supposed to add the graph (value) of the measurement (which is given for each point) to the trajectory (which is not always a straight line). I will added a little sketch to explain what i want. The red arrow shows what i want to add to my plot and the green shows what i have.
You can always transform your data yourself: (using the same notation as #Shai)
x = 0:0.1:10;
y = x;
m = 10*sin(x);
So what you need is the vector normal to the curve at each datapoint:
dx = diff(x); % backward finite differences for 2:end points
dx = [dx(1) dx]; % forward finite difference for 1th point
dy = diff(y);
dy = [dy(1) dy];
curve_tang = [dx ; dy];
% rotate tangential vectors 90° counterclockwise
curve_norm = [-dy; dx];
% normalize the vectors:
nrm_cn = sqrt(sum(abs(curve_norm).^2,1));
curve_norm = curve_norm ./ repmat(sqrt(sum(abs(curve_norm).^2,1)),2,1);
Multiply that vector with the measurement (m), offset it with the datapoint coordinates and you're done:
mx = x + curve_norm(1,:).*m;
my = y + curve_norm(2,:).*m;
plot it with:
figure; hold on
axis equal;
scatter(x,y,[],m);
plot(mx,my)
which is imo exactly what you want. This example has just a straight line as coordinates, but this code can handle any curve just fine:
x=0:0.1:10;y=x.^2;m=sin(x);
t=0:pi/50:2*pi;x=5*cos(t);y=5*sin(t);m=sin(5*t);
If I understand your question correctly, what you need is to rotate your actual data around an origin point at a certain angle. This is pretty simple, as you only need to multiply the coordinates by a rotation matrix. You can then use hold on and plot to overlay your plot with the rotated points, as suggested in the comments.
Example
First, let's generate some data that resembles yours and create a scatter plot:
% # Generate some data
t = -20:0.1:20;
idx = (t ~= 0);
y = ones(size(t));
y(idx) = abs(sin(t(idx)) ./ t(idx)) .^ 0.25;
% # Create a scatter plot
x = 1:numel(y);
figure
scatter(x, x, 10, y, 'filled')
Now let's rotate the points (specified by the values of x and y) around (0, 0) at a 45° angle:
P = [x(:) * sqrt(2), y(:) * 100] * [1, 1; -1, 1] / sqrt(2);
and then plot them on top of the scatter plot:
hold on
axis square
plot(P(:, 1), P(:, 2))
Note the additional things have been done here for visualization purposes:
The final x-coordinates have been stretched (by sqrt(2)) to the appropriate length.
The final y-coordinates have been magnified (by 100) so that the rotated plot stands out.
The axes have been squared to avoid distortion.
This is what you should get:
It seems like you are interested in 3D plotting.
If I understand your question correctly, you have a 2D curve represented as [x(t), y(t)].
Additionally, you have some value m(t) for each point.
Thus we are looking at the plot of a 3D curve [x(t) y(t) m(t)].
you can easily achieve this using
plot3( x, y, m ); % assuming x,y, and m are sorted w.r.t t
alternatively, you can use the 3D version of scatter
scatter3( x, y, m );
pick your choice.
Nice plot BTW.
Good luck with your thesis.