I want to draw ellipse in matlab. I have ellipse parameters [duplicate] - matlab

This question already has an answer here:
Draw ellipse in MATLAB
(1 answer)
Closed 6 years ago.
Any ellipse can be uniquely defined by five parameters i.e. center x0 and y0, semi-major aixs length a, semi minor axis length b, and orientation angle theta. I have parameters x0, y0, a, b, and theta. How can I exactly draw the ellipse?

Some researches are necessary before to ask this kind of question. Mainly if the question was asked too much time.
You can do something like this :
Let (x1,y1) and (x2,y2) be the coordinates of the two vertices of the ellipse's major axis, and let e be its eccentricity.
a = 1/2*sqrt((x2-x1)^2+(y2-y1)^2);
b = a*sqrt(1-e^2);
t = linspace(0,2*pi);
X = a*cos(t);
Y = b*sin(t);
w = atan2(y2-y1,x2-x1);
x = (x1+x2)/2 + X*cos(w) - Y*sin(w);
y = (y1+y2)/2 + X*sin(w) + Y*cos(w):
plot(x,y,'y-')
axis equal
I don't have time to test it, but it should work.
Next time, please read this section : How do I ask a good question

Related

Matlab - intersection coordinates of lines based on their coordinates [duplicate]

This question already has answers here:
Find point of intersection between two vectors in MATLAB
(4 answers)
Closed 5 years ago.
I have 2 lines coordinates (x1,y1 x2,y2 and x3,y3 x4,y4), how can I calculate the intersection coordinates without plotting them?
You could use the function polyxpoly for getting the intersection points.
See here for documentation and further information.
Here is a short example:
start1 = [1;1];
end1 = [3;3];
line1 = [start1, end1];
start2 = [1;3];
end2 = [2;1];
line2 = [start2,end2];
[xi, yi] = polyxpoly(line1(1,:), line1(2,:), line2(1,:), line2(2,:));
This will give you the intersection point xi and yi.
Note that this function is capable of a lot more than dealing with simple lines, such as boxes, intersection segments, etc.
The intersection point will be (x,y) = ((b1-b)/(1-a1), (a1*b-b1*a)/(a1-a))
where a = (y1-y2)/(x1-x2);
a1 = (y3-y4)/(x3-x4);
b = y1 - x1*(y1-y2)/(x1-x2);
b1 = y3 - x3*(y3-y4)/(x3-x4)
You can check the algebra by following these steps:
1) find the equation for the line passing by (x1,y1) and (x2,y2) and another equation for the line passing by the other two points;
2) force equality onto the two equations and you will have the intersection point

If two rectangles intersection is zero in matlab [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Knowing center of the two rectangles and their angle by x axis (horizontal axis), how can one recognize if their intersection is zero or not in Matlab? Any answers containing this information is highly appreciated. Width and length of rectangles are also known
This is a programming problem if you want to solve it numerically. For exact solutions, you could use geometrical equations.
The first problem: defining a rectangle's corners from its width, height and centre:
C1 = [0, 0]; % Centre of rectangle 1 (x,y)
C2 = [1, 1]; % Centre of rectangle 2 (x,y)
W1 = 5; W2 = 3; % Widths of rectangles 1 and 2
H1 = 2; H2 = 3; % Heights of rectangles 1 and 2
% Define the corner points of the rectangles using the above
R1 = [C1(1) + [W1; W1; -W1; -W1]/2, C1(2) + [H1; -H1; -H1; H1]/2];
R2 = [C2(1) + [W2; W2; -W2; -W2]/2, C2(2) + [H2; -H2; -H2; H2]/2];
Next problem is to create many points which represent the edges of the rectangles. You could instead generate many points within the rectangles if you wanted to look at intersecting areas.
n = 1000; % Define some number of points to use
% Use interp1 to interpolate around the rectangles
R1points = interp1(1:5, [R1; R1(1,:)], linspace(1,5,n));
R2points = interp1(1:5, [R2; R2(1,:)], linspace(1,5,n));
Then rotate the rectangles:
a1 = deg2rad(0); a2 = deg2rad(30); % angles of rotation for rectangle 1 and 2 respectively
R1rotated(:,1) = (R1points(:,1)-C1(1))*cos(a1) - (R1points(:,2)-C1(2))*sin(a1) + C1(1);
R1rotated(:,2) = (R1points(:,1)-C1(1))*sin(a1) + (R1points(:,2)-C1(2))*cos(a1) + C1(2);
R2rotated(:,1) = (R2points(:,1)-C2(1))*cos(a2) - (R2points(:,2)-C2(2))*sin(a2) + C2(1);
R2rotated(:,2) = (R2points(:,1)-C2(1))*sin(a2) + (R2points(:,2)-C2(2))*cos(a2) + C2(2);
Finally, check intersection with inpolygon:
in1 = inpolygon(R1rotated(:,1), R1rotated(:,2), R2rotated(:,1), R2rotated(:,2));
in2 = inpolygon(R2rotated(:,1), R2rotated(:,2), R1rotated(:,1), R1rotated(:,2));
If nnz(in1)>0 or nnz(in2)>0 then you have an intersection! Visualise it using scatter:
hold on
scatter(R2rotated(:,1), R2rotated(:,2), '.b')
scatter(R2rotated(in2,1), R2rotated(in2,2), 'xc')
scatter(R1rotated(:,1), R1rotated(:,2), '.r')
scatter(R1rotated(in1,1), R1rotated(in1,2), 'xg')
Result:

Integrating over a matrix of points [duplicate]

This question already has answers here:
How to integrate over a discrete 2D surface in MATLAB?
(3 answers)
Closed 8 years ago.
I have a matrix of points F(x,y) = z, but I have no expression for F(x,y).
x is from [0-2pi] and y is from [0-pi]. For each pair of "coordinates", I have a value of z.
I would like to perform a double integration from 0-2pi and 0-pi of F. Can I do this computationally (MatLab) without having an analytical expression?
Thanks!
Assuming that the (x,y) grid is uniform, you can approximate the integral by a 2D-Riemman sum as follows:
result = sum(z(:))*delta_x*delta_y;
where delta_x, delta_y are the grid spacings in the x and y directions. In your case these can be computed as
delta_x = 2*pi/numel(x); %// or 2*pi/(numel(x)-1)
delta_y = pi/numel(x); %// or pi/(numel(x)-1)
A perhaps more intuitive interpretation: compute the mean value of the function and multiply by the area of the (x,y) domain:
result = sum(z(:))/(numel(x)*numel(y)) * 2*pi^2; %// or replace numel(x)*numel(y)
%// by numel(z)

How to plot an cylindrical coordinates equation in cartesian [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have this equation r=z*cos(theta) and I need to plot it in Cartesian coordinates in Matlab. How can I do this?
First off, the definition of your cylindrical co-ordinates is wrong. Given the azimuthal sweep around the z axis theta as well as the radius of the cylinder r, the Cartesian co-ordinates within a cylinder is defined as:
x = r*cos(theta)
y = r*sin(theta)
z = z
Therefore, you would need to define a grid of co-ordinates for r, theta and z, use these co-ordinates and plug them into the above code, then draw them. I would recommend you use plot3 as you want to plot 3D points in Cartesian space. Also, use meshgrid to define your grid of points.
As such, the radius of a cylinder is (usually) constant when you're drawing it, and theta and z are the quantities you are varying. Let's assume that -2 <= z <= 2 and r = 2. We know that to create a cylinder, we have to sweep around a circle from 0 <= theta <= 2*pi. Therefore, do something like this:
%// Define (r,theta,z)
[theta, z] = meshgrid(0:0.001:2*pi, -2:0.001:2);
r = 2*ones(size(theta));
%// Calculate x and y. z was calculated earlier
x = r.*cos(theta);
y = r.*sin(theta);
%// Plot the points
plot3(x(:), y(:), z(:), 'b.');
grid;
view(-48,60); %// Adjust viewing angle
This is what I get:
You can certainly use more efficient techniques, like what Kamtal has suggested with pol2cart, but you said you wanted the actual Cartesian co-ordinates, and so x, y and z contains those co-ordinates in 3D space for you. I'm assuming you want these for further processing.
Good luck!
n = linspace(-pi,pi,20);
m = linspace(0,1,20);
[theta,z] = meshgrid(n,m);
r = z .* cos(theta);
[X,Y,Z] = pol2cart(theta,r,z);
surf(X,Y,Z)
axis equal
If you change it to r = 1 .* cos(theta); then you will get a cylinder,

Matlab, hamiltonian system, plotting and energy calculation with Euler (backwards, forwards, mid point and semi-implicit) [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
This is the 2 body kepler problem.
The Hamiltonian is
This is supposed to be an "ellipse". How in the name of all that is holy can it be an ellipse when the initial values only have velocity components on one axis, and starting coordinates are on the same axis?
How do you plot the numerical solution using backwards euler?
I convert the two second-order equations into four first-order equations and solve using ode23 (not backwards Euler, but a different numerical method).
odefun = #(t, y) [y(3);
y(4);
-y(1) / (y(1)^2 + y(2)^2)^(3/2);
-y(2) / (y(1)^2 + y(2)^2)^(3/2)]
p0 = [1 0]; % initial position
v0 = [0 1]; % initial velocity
[t y] = ode23(odefun, [0 20], [p0 v0])
comet(y(:,1), y(:,2))
You can see now that the path traced is an ellipse (actually a circle for my initial velocity). You can play with the inital conditions (v0 = [0 1.1] gives you an ellipse).
Maybe i missunderstood what the system actually means, but what i see is a system with two bodies only under the influence of each others gravity. Starting position for body 1 is not a vector, but only a scalar, so it can only be located on the X axis. In this case it's coordinates are x = 1-a (0 < a < 1). Body 2 has it's starting position on x=0. Now, there is no force vectors pointing them away from the x axis, since the gravity gradient always is parallell to that same axis. The starting velocity is of course also a scalar, so there is no initial veolcity pointing them anywhere but on a parallell trajectory to the x axis.
When you say "this forms an ellipse", maybe i'm looking at the wrong coordinate system. I did the forward Euler plots as follows:
clear all, close all, clc
stl= 0.0005; % steplength
nos = 50000; % number of steps
q1 = zeros(1,nos);
q2 = zeros(1,nos);
q1v = zeros(1,nos);
q2v = zeros(1,nos);
q1a = zeros(1,nos);
q2a = zeros(1,nos);
% Starting positions
a=0.5;
q1(1) = 1-a;
q2(1) = 0;
q1v(1) = 0;
q2v(1) = sqrt((1+a)/(1-a));
% P is inertia, and has the same vector components as the body velocity
for i=1:nos-1
[q1a(i),q2a(i)] = u1FUNC(q1(i),q2(i));
q1v(i+1) = q1v(i) + q1a(i).*stl;
q2v(i+1) = q2v(i) + q2a(i).*stl;
q1(i+1) = q1(i) + q1v(i+1)*stl;
q2(i+1) = q2(i) + q2v(i+1)*stl;
end
plot(q1)
hold on
plot(q2,'r')
u1FUNC
function [a,b] = func(c,d)
% [a,b] = [q1'',q2'']
% [c,d] = [q1,q2]
a = -c/(((c^2)+(d^2))^(3/2));
b = -d/(((c^2)+(d^2))^(3/2));
end
Now, this code gives a plot where q1 and q2 are plotted with q1 and q2 on the y axis and time on the x axis, which seems logical to me, if q1 and q2 decribes the distance for the body in question from the origo in a non-moving reference system. But how do you make it look like an ellipse?