Saving plotted function as 3d matrix - matlab

In matlab I would like to get below defined function's 3D matrix representation. Function is f(x,y) = cos(x + y)^2. Range for x is from 0 to PI, and for y it is also 0 to PI. I wonder if this is possible?
For plotting this function in matlab I usedMuPad matlab's app.
plotfunc3d(cos(x + y)^2, x = 0..PI, y = 0..PI)

Hi with the following script I got the 3D plot of your function.
step = 0.05 ;
x = 1;
y = 1;
image = zeros(length(1:step:pi),length(1:step:pi));
for i =1:step:pi
for j=1:step:pi
image(x,y) = cos(i + j)^2;
x = x+1;
end
x = 1;
y = y + 1;
end
surf(image)

Related

Unknown error in code to find the angle necessary for a rocket to land on the moon

I need to find the angle of a rocket given the conditions in the code that enables it to land on the moon. The moon is taken as stationary at the given coordinates. I assumed the only way to try would just be calculating the different velocity components and then inputting them for each angle from the minimum to the maximum, which is what I've tried to do.
Not sure why this isn't working - it just runs forever and doesn't output anything.
Thank you.
I have
v = 0.0066;
for angle = 0:.5:180
init_vel = [v*cosd(angle), v*sind(angle)];
init_pos = [3.7,0];
t = 10;
moon_pos = [0,222];
%simulate rocket
[tout, pos] = simulaterocketmovement(init_pos, init_vel, moon_pos, t);
if(length(tout)<99999)
break;
end
end
disp(angle);
plot(pos(:,1),pos(:,2));
where
function [ tout , pos ] = simulaterocketmovement ( init_pos , init_vel , moon_pos , t)
%Defining initial variables for later use
G = 9.63*10^-7;
Me = 83.3;
%Defining the two vectors we will use
x = init_pos(1);
y = init_pos(2);
vx = init_vel(1);
vy = init_vel(2);
%Need to create a seperate function that integrates to find the
%acceleration using Euler's second order method.
function a = acc(x,y)
ax = -(G*Me*x)/((x^2 + y^2)^1.5) - (G*(x-moon_pos(1)))/(((moon_pos(1)-x)^2 + (moon_pos(2)-y)^2)^1.5);
ay = -(G*Me*y)/((x^2 + y^2)^1.5) - (G*(y-moon_pos(2)))/(((moon_pos(1)-x)^2 + (moon_pos(2)-y)^2)^1.5);
%After finding the vector components, we put them in an acceleration vector.
a = [ax,ay];
end
%Now we find the values which result in the rocket landing on the moon. The
%range of values lie between xmin and xmax, and ymin and ymax. The +/-
%represents that the rocket can land anywhere on the moon's surface.
xmax = moon_pos(1) + 1;
xmin = moon_pos(1) - 1;
ymax = moon_pos(2) + 1;
ymin = moon_pos(2) - 1;
%For each time taken, to find the x and y values we need to use a while
%loop which works until the rocket is in the range of values for it to
%land on the moon.
while((x(end) > xmax) || (x(end) < xmin) || (y(end) < ymin) || (y(end) > ymax) )
%We assign temporary new values of x and y.
x(end+1) = x(end) + t*vx;
y(end+1) = y(end) + t*vy;
%Then we find the values of acceleration for both the old and new x and y
aold = acc(x(end-1), y(end-1));
anew = acc(x(end), y(end));
%Using this to find the new velocities
vxnew = vx + t*(aold(1)+anew(1))/2;
vynew = vy + t*(aold(2)+anew(2))/2;
%Final, more accurate values for x and y
x(end) = x(end-1) + t*(vxnew + vx)/2;
y(end) = y(end-1) + t*(vynew + vy)/2;
%And updating these as the new velocities
vx = vxnew;
vy = vynew;
end
%Then we construct a vector for the time steps, for the entire journey.
tout = 0:t:((length(x)-1)*t);
%And then create a position vector which includes the x and y positions.
pos = zeros(length(x),2);
pos(:,1) = x;
pos(:,2) = y;
end

Image Rotation in Three Dimensions (Matlab)

I am trying to rotate a 2D image in Matlab xyz 3-D space. I want to rotate the image I by an angle θ=i about the x-axis. I am doing this by means of multiplication with the rotation matrix (as defined here):
This is my code:
x = linspace(-1,1,size(I,1));
[x0,y0,z0] = meshgrid(x,x,1);
R = [1 0 0; 0 cosd(-i) -sind(-i); 0 sind(-i) cosd(-i)];
xy = [x0(:),y0(:), z0(:)]*R';
X = reshape(xy(:,1),size(x0));
Y = reshape(xy(:,2),size(y0));
Z = reshape(xy(:,3),size(z0));
rotatedI = interp3(x0,y0,z0,I,X,Y,Z, 'nearest');
rotatedI(isnan(rotatedI)) = 0;
The error that I am getting is in the interpolation line:
Error using griddedInterpolant
Interpolation requires at least two sample points in each
dimension.
So what exactly is the problem here and how can I fix it?
I am confused because this code works fine when specialized to two dimensions. Any explanation is much appreciated.
the problem is that your input z grid z0 contains a single value (1), while your desired z grid Z contains multiple values. therefore there is no way to interpolate this single value into multiple ones.
you can assign the input z grid with multiple values so that it will be possible to interpolate.
I = im2double(imread('cameraman.tif'));
I = cat(3,I,I,I);
i = 10;
x = linspace(-1,1,size(I,1));
[x0,y0,z0] = meshgrid(x,x,-1:1); % 3 z values
R = [1 0 0; 0 cosd(-i) -sind(-i); 0 sind(-i) cosd(-i)];
xy = [x0(:),y0(:), z0(:)]*R;
X = reshape(xy(:,1),size(x0));
Y = reshape(xy(:,2),size(y0));
Z = reshape(xy(:,3),size(z0));
rotatedI = interp3(x0,y0,z0,I,X,Y,Z, 'nearest');
rotatedI(isnan(rotatedI)) = 0;
imshow(rotatedI);
in addition, you can use rotx, roty, and rotz to generate rotation matrices easily.
UPDATE
if you want to rotate the image 2D plane in the 3D space you don't need to use interpolation and can do something like:
I = im2double(imread('cameraman.tif'));
i = 10;
x = linspace(-1,1,size(I,1));
[x0,y0,z0] = meshgrid(x,x,-1:1); % 3 z values
[x0,y0,z0] = meshgrid(x,x,1); % 3 z values
R = [1 0 0; 0 cosd(-i) -sind(-i); 0 sind(-i) cosd(-i)];
xy = [x0(:),y0(:), z0(:)]*R;
X = reshape(xy(:,1),size(x0));
Y = reshape(xy(:,2),size(y0));
Z = reshape(xy(:,3),size(z0));
warp(X,Y,Z,I);
grid on

Plotting a 2d vector on 3d axes in matlab

I have three vectors x,y,t. For each combination x,y,t there is a (u,v) value associated with it. How to plot this in matlab? Actually I'm trying to plot the solution of 2d hyperbolic equation
vt = A1vx + A2vy where A1 and A2 are 2*2 matrices and v is a 2*1 vector. I was trying scatter3 and quiver3 but being new to matlab I'm not able to represent the solution correctly.
In the below code I have plot at only a particular time-level. How to show the complete solution in just one plot? Any help?
A1 = [5/3 2/3; 1/3 4/3];
A2 = [-1 -2; -1 0];
M = 10;
N = 40;
delta_x = 1/M;
delta_y = delta_x;
delta_t = 1/N;
x_points = 0:delta_x:1;
y_points = 0:delta_y:1;
t_points = 0:delta_t:1;
u = zeros(M+1,M+1,N+1,2);
for i=1:M+1,
for j=1:M+1,
u(i,j,1,1) = (sin(pi*x_points(i)))*sin(2*pi*y_points(j)) ;
u(i,j,1,2) = cos(2*pi*x_points(i));
end
end
for j=1:M+1,
for t=1:N+1,
u(M+1,j,t,1) = sin(2*t);
u(M+1,j,t,2) = cos(2*t);
end
end
for i=1:M+1
for t=1:N+1
u(i,1,t,1) = sin(2*t);
u(i,M+1,t,2) = sin(5*t) ;
end
end
Rx = delta_t/delta_x;
Ry = delta_t/delta_y;
for t=2:N+1
v = zeros(M+1,M+1,2);
for i=2:M,
for j=2:M,
A = [(u(i+1,j,t-1,1) - u(i-1,j,t-1,1)) ; (u(i+1,j,t-1,2) - u(i-1,j,t-1,2))];
B = [(u(i+1,j,t-1,1) -2*u(i,j,t-1,1) +u(i-1,j,t-1,1)) ; (u(i+1,j,t-1,2) -2*u(i,j,t-1,2) +u(i-1,j,t-1,2))];
C = [u(i,j,t-1,1) ; u(i,j,t-1,2)];
v(i,j,:) = C + Rx*A1*A/2 + Rx*Rx*A1*A1*B/2;
end
end
for i=2:M,
for j=2:M,
A = [(v(i,j+1,1) - v(i,j-1,1)) ; (v(i,j+1,2) - v(i,j-1,2)) ];
B = [(v(i,j+1,1) - 2*v(i,j,1) +v(i,j-1,1)) ; (v(i,j+1,2) - 2*v(i,j,2) +v(i,j-1,2))];
C = [v(i,j,1) ; v(i,j,2)];
u(i,j,t,:) = C + Ry*A2*A/2 + Ry*Ry*A2*A2*B/2;
end
end
if j==2
u(i,1,t,2) = u(i,2,t,2);
end
if j==M
u(i,M+1,t,1) = u(i,M,t,1);
end
if i==2
u(1,j,t,:) = u(2,j,t,:) ;
end
end
time_level = 2;
quiver(x_points, y_points, u(:,:,time_level,1), u(:,:,time_level,2))
You can plot it in 3D, but personally I think it would be hard to make sense of.
There's a quiver3 equivalent for your plotting function. z-axis in this case would be time (say, equally spaced), and z components of the vectors would be zero. Unlike 2D version of this function, it does not support passing in coordinate vectors, so you need to create the grid explicitly using meshgrid:
sz = size(u);
[X, Y, Z] = meshgrid(x_points, y_points, 1:sz(3));
quiver3(X, Y, Z, u(:,:,:,1), u(:,:,:,2), zeros(sz(1:3)));
You may also color each timescale differently by plotting them one at a time, but it's still hard to make sense of the results:
figure(); hold('all');
for z = 1:sz(3)
[X, Y, Z] = meshgrid(x_points, y_points, z);
quiver3(X, Y, Z, u(:,:,z,1), u(:,:,z,2), zeros([sz(1:2),1]));
end

Finding the intersection points of ray-5th order polynomial

I am doing ray tracing and I have A screen described in the world coordinates as Matrices(I had before the X,Y,Z in the screen coordinates and by using transformation and rotation I got it in the world coordinates)
Xw (NXM Matrix)
Yw (NXM Matrix)
Zw (I have got this polynomial (5th order polynomial)by fitting the 3D data Xw and Yw. I have it as f(Xw,Yw))
I have the rays equations too described as usual:
X = Ox + t*Dx
Y = Oy + t*Dy
Z = Oz + t*Dz %(O is the origin point and D is the direction)
So what I did is that I replaced the X and Y in the Polynomial equation f(Xw,Yw) and solved it for t so I can then get the intersection point.
But apparently the method that I used is wrong(The intersection points that I got were somewhere else).
Could any one please help me and tell me what is the mistake. Please support me.
Thanks
This is part of the code:
X_World_coordinate_scr = ScreenXCoordinates.*Rotation_matrix_screen(1,1) + ScreenYCoordinates.*Rotation_matrix_screen(1,2) + ScreenZCoordinates.*Rotation_matrix_screen(1,3) + Zerobase_scr(1);
Y_World_coordinate_scr = ScreenXCoordinates.*Rotation_matrix_screen(2,1) + ScreenYCoordinates.*Rotation_matrix_screen(2,2) + ScreenZCoordinates.*Rotation_matrix_screen(2,3) + Zerobase_scr(2);
Z_World_coordinate_scr = ScreenXCoordinates.*Rotation_matrix_screen(3,1) + ScreenYCoordinates.*Rotation_matrix_screen(3,2) + ScreenZCoordinates.*Rotation_matrix_screen(3,3) + Zerobase_scr(3); % converting the screen coordinates to the world coordinates using the rotation matrix and the translation vector
polymodel = polyfitn([X_World_coordinate_scr(:),Y_World_coordinate_scr(:)],Z_World_coordinate_scr(:),5); % using a function from the MAtlab file exchange and I trust this function. I tried it different data and it gives me the f(Xw,Yw).
ScreenPoly = polyn2sym(polymodel); % Function from Matlab file exchange to give the symbolic shape of the polynomial.
syms X Y Z t Dx Ox Dy Oy oz Dz z;
tsun = matlabFunction(LayerPoly, 'vars',[X,Y,Z]); % just to substitue the symboles from X , Y and Z to (Ox+t*Dx) , (Oy+t*Dy) and (Oz+t*Dz) respectively
Equation = tsun((Ox+t*Dx),(Oy+t*Dy),(Oz+t*Dz));
Answer = solve(Equation,t); % solving it for t but the equation that it is from the 5th order and the answer is RootOf(.... for z)
a = char(Answer); % preparing it to find the roots (Solutions of t)
R = strrep(a,'RootOf(','');
R1 = strrep(R,', z)','');
b = sym(R1);
PolyCoeffs = coeffs(b,z); % get the coefficient of the polynomail
tfun = matlabFunction(PolyCoeffs, 'vars',[Ox,Oy,oz,Dx,Dy,Dz]);
tCounter = zeros(length(Directions),1);
NaNIndices = find(isnan(Surface(:,1))==1); %I have NaN values and I am taking them out
tCounter(NaNIndices) = NaN;
NotNaNIndices = find(isnan(Surface(:,1))==0);
for i = NotNaNIndices' % for loop to calc
OxNew = Surface(i,1);
OyNew = Surface(i,2);
OzNew = Surface(i,3);
DxNew = Directions(i,1);
DyNew = Directions(i,2);
DzNew = Directions(i,3);
P = tfun(OxNew,OyNew,OzNew ,DxNew,DyNew,DzNew);
t = roots(P);
t(imag(t) ~= 0) = []; % getting rid of the complex solutions
tCounter(i) = t;
end
Please support
Thanks in advance

Matlab Differentiation

I need to write a for loop in matlab to solve a derivative using the forward difference method. The function to derive is 10+15x+20x^2 from 0 to 10 using steps of 0.25. I have tried using
h=.25;
x=[0:h:10];
y = 10+15*x+20*x.^2;
y(1) = 45; size(x)
for i=2:47,
y(i) = y(i-1) + h*(15+40*x);
end
I'd do like this, as a start,
h=.25;
x=[0:h:10];
y = 10+15*x+20*x.^2;
diff(y)./diff(x)
or, as alternative,
syms x;
y = 20.*x.^2 + 15.*x + 10;
dy = diff(y,1);
h=.25;
xx=[0:h:10];
res = subs(dy,xx);