Numerical 2D integration in Matlab - matlab

Let's say I have the following vector field of force:
x = [-1 -0.5 0 0.5 1];
y = x;
dx = repmat([1,0.5,0,-0.5,-1],5,1);
dy = repmat([1,0.5,0,-0.5,-1]',1,5);
[X,Y] = meshgrid(x,y);
I want to obtain an energy landscape from this field.
If I just sum vectors I have the asymmetric picture:
imagesc(x,y,cumsum(dx,2)+cumsum(dy,1))
What is wrong in my code?

Related

Extrinsic Matrix from camera pose

I'm trying to compute the extrinsic matrix from the pose (position and orientation) of the camera given in world coordinates. I used the following to compute the extrinsic matrix,
T = [R -Rt; 0 1] 3x4 Matrix
The rotation(theta2) of the camera is about the Y-axes of the camera i.e. yaw about the camera axis. The translation vector is [x, y, z] in meters.
My Setup
theta1 = deg2rad(theta1);
RW1 = [[cos(theta1), 0, sin(theta1)];
[0,1, 0];
[-sin(theta1), 0, cos(theta1)]];
tW1 = [0; 0; 0];
TW1 = [RW1 tW1; 0 0 0 1];
theta2 = deg2rad(theta2);
R12 = [[cos(theta2), 0, sin(theta2)];
[0,1, 0];
[-sin(theta2), 0, cos(theta2)]];
t12 = [x; y; z];
T12 = [R12 t12; 0 0 0 1];
P1 = K * TW1;
P2 = K * T12;
K - Camera Intrinsic Matrix
Is this the right way to calculate the extrinsic matrix? Am I missing any transformations between the world and the camera frame?
I'm trying to implement this https://www.cs.cmu.edu/~16385/s17/Slides/11.4_Triangulation.pdf and for the camera matrix, I followed this https://www.cs.cmu.edu/~16385/s17/Slides/11.1_Camera_matrix.pdf.

2d pchip in Matlab

I know values of z on a (x,y) meshgrid, defined by xgrid, ygrid.
xgrid, and ygrid are not uniform grids.
I need to interpolate value of z off the grid points.
I need interpolants similar to 'pchip', because the problem at hand requires shape-preseving, and 'pchip' has better shape-preserving properties than e.g. 'spline'.
My current code:
xgrid = [0.01 0.1 0.5 1]';
ygrid = [0.01 0.1 0.5 1];
z = 0.01./(repmat(xgrid,[1 4]).*repmat(ygrid,[4 1]));
x1 = 0.05;
y1 = 0.05;
[xmesh,ymesh] = ndgrid(xgrid,ygrid);
interpolant1 = griddedInterpolant(xmesh,ymesh,z,'cubic');
z1 = interpolant1(x1, y1);
for ix = 1:length(xgrid);
interpolant2(ix) = interp1(ygrid, z(ix,:), y1, 'pchip');
end
z1 = interp1(xgrid, interpolant2, x1, 'pchip');
In my example, interpolant1 will use 'spline' instead of 'cubic' and seems less accurate due to overshooting (http://blogs.mathworks.com/cleve/2012/07/16/splines-and-pchips/). However, interpolant1 is much faster.
My main question: what can I do to make my code for interpolant2 more efficient? vectorization? use an alternative to interp1?

MATLAB projectile motion with air resistance

I am trying to calculate maximum distance and maximum height of a projectile for an angle theta below.
I assume my way of plotting the graphs of distance against theta and height against theta (on the same graph) are wrong. Any pointers on that will be helpful.
e=100;
m = 1;
g = 9.8;
cd = 0.55;
r = 0.02;
p = 1.21;
a = pi*r^2;
v = sqrt((2*e)/m);
k = (1/2)*cd*a*p;
% For loop to calculate Distance and
for theta = (0:pi/4);
vx = v*cos(theta);
vy = v*sin(theta);
t = sqrt(m/(g*k))*atan(vy*sqrt(k/(m*g)));
x = (m/k)* log((1/vx)+(k/m)*t) - log(1/(vx));
h = (m/k)*(log(cos(atan(vy*sqrt(k/(m*g))-sqrt((g*k)/m)*t))-log(cos((atan(vy*sqrt(k/m*g)))))));
plot (x, theta);
plot (h, theta);
end
You don't need to loop through different values of theta. Try this instead:
theta = (0:0.01:pi/4); % theta = [0 0.01 0.02 0.03 ... pi/4]
vx = v*cos(theta);
vy = v*sin(theta);
t = sqrt(m/(g*k)) * atan(vy .* sqrt(k/(m*g))); % element wise matrix multiplication
x = (m/k)* log((1./vx)+(k/m)*t) - log(1./(vx));
h = (m/k)*(log(cos(atan(vy .* sqrt(k/(m*g))- sqrt((g*k)/m)*t))-log(cos((atan(vy .* sqrt(k/m*g)))))));
plot (x, theta);
plot (h, theta);
x vs theta:
Also try this:
MATLAB: Creating a Function to Plot Projectile with Drag
MATLAB: Numerical approximation of projectile motion with air resistance
Hope this helps!
The variable t is not defined. Also the loop variable should be integer, for example:
N = 10;
for i = 0:N-1
theta = pi/4*i/(N-1)
(...)
end

MATLAB - How to rotate an orbit/sphere using rotation matrix?

I am trying to rotate an "orbit" using rotation matrix given below:
[cos(angle) -sin(angle) 0;
sin(angle) cos (angle) 0;
0 0 1 ]
First thing I thought I should do was to use sphere():
[x y z] = sphere;
Then concatenate x, y, and z together in a vector:
xyz = [x; y; z];
rotatMat = [cos(angle) -sin(angle) 0; sin(angle) cos (angle) 0; 0 0 1 ];
multiply rotation matrix and xyz to rotate an orbit:
rotated = rotatMat .* xyz;
However, xyz turns out to be 62x22 dimension while my rotatMat is only 3x3 so I cannot multiply them together.
How can i fix this issue?
Thank you in advance.
You have to use the * operator for matrix multiplication, and not .* which is for element-wise multiplication.
Moreover, your xyz matrix should be of size n-by-3 (and not 62-by-22) and you have to use xyz*rotatMat' to match the dimensions correctly. Alternatively, you can have xyz of size 3-by-n and use the syntax rotatMat*xyz.
Best,
xyz = [x(:) y(:) z(:)].'; %'// put x, y, z as rows of a matrix
xyz_rotated = rotatMat*xyz %// use matrix multiplication
x_rotated = reshape(xyz_rotated(1,:), size(x)); %// reshape transformed rows
y_rotated = reshape(xyz_rotated(2,:), size(x));
z_rotated = reshape(xyz_rotated(3,:), size(x));

Plot 3D plane given equations in matlab

I want to plot 2 3D planes for the equations given below :
x + y + z = 1
2x - y = 0
For 1st equation, I plotted it using meshgrid as :
[x y] = meshgrid(-5:0.5:5);
z = 1 - x - y
mesh(x,y,z)
But for 2nd equation, z is not given i.e. z can be anything, then how do I plot plane for this ?
The comments are correct. It is more of a math problem. You draw a line 2x - y = 0 and translate it for any z value to create a plane.
[x, y] = meshgrid(-5:0.5:5);
Zv = #(x,y) 1 - x - y;
mesh(x,y,Zv(x,y));
hold on
[x, z] = meshgrid(-5:0.5:5);
Yv = #(x) 2*x;
mesh(x,Yv(x),z);
hold off