I have a Leslie Matrix
LeslieMatrixA = [0 0.4 0.7 0.5;
x 0 0 0;
0 0.8 0 0;
0 0 0.7 0]
and an initial population vector [10;10;10;10] where 0.7<=x<=0.9.
How can I create MATLAB code to show behaviour of the population over time?
Any help appreciated!
Thanks.
If you just want to show how the population changes you could just plot it right?
x=0.7;
LeslieMatrixA = [0 0.4 0.7 0.5; x 0 0 0; 0 0.8 0 0; 0 0 0.7 0];
P = [10;10;10;10];
for ct = 1:10
bar([1:4],P)
title(sprintf('iteration: %.0f',ct))
pause
P=LeslieMatrixA*P;
end
Related
I am trying to compute dynamics of 3 DOF robot using Robotics Toolbox by executing this code:
robot.accel(q, zeros(1,3), zeros(1,3))
But I am getting this error: Assignment has more non-singleton rhs dimensions than non-singleton subscripts
My robot is:
L(1)= Link([0 0.03 0 -pi/2]);
L(2)= Link([0 0 0.28 0]);
L(3)= Link([0 0 0.2 0]);
L(1).m = 1;
L(2).m = 4;
L(3).m = 3;
L(1).r=[0 0 -0.015];
L(2).r=[0.14 0 0];
L(3).r=[0.1 0 0];
ax1=0.03; ay1=0.03; az1=0.03;
ax2=0.28; ay2=0.05; az2=0.05;
ax3=0.2; ay3=0.05; az3=0.05;
I1=1/12*[ay1^2+az1^2 0 0; 0 ax1^2+az1^2 0; 0 0 ax1^2+ay1^2];
I2=4/12*[ay2^2+az2^2 0 0; 0 ax2^2+az2^2 0; 0 0 ax2^2+ay2^2];
I3=3/12*[ay3^2+az3^2 0 0; 0 ax3^2+az3^2 0; 0 0 ax3^2+ay3^2];
L(1).I=I1;
L(2).I=I2;
L(3).I=I3;
q=[0 0 0]
robot=SerialLink(L);
The error was due to no mention of motor inertias. I have set them to:
for i=1:3
robot.links(i).Jm = 2.1184*10^-4;
end
and the error is gone while using robot.accel command.
I want to plot lat/lon contours at the south pole, my data consists of grid cells, each with a lat and lon value. My problem is where the longitudes "meets" each other, as you can see in the picture, the vertical longitude in the lower part of the picture have a lot of lines on top of each other.
Does anyone know how to remove them? Here is my code:
coldiv = -57.5:2.5:-7.5;
min_pr = coldiv(1); max_pr = coldiv(end);
cmap = [0 0 0.4; 0 0 0.7; 0 0 1; 0 0.2 1; 0 0.4 1; 0 0.6 1; 0 0.8 1; 0 1 1;
0 1 0; 0.6 0.8 0; 0.6 0.6 0; 0.6 0.4 0; 0.6 0.2 0; 1 0 0];
x = linspace(1,length(coldiv)-2,length(cmap));
xi = 1:length(coldiv)-2;
cmap = interp1(x,cmap,xi);
cmap = [0. 0. 0.; cmap];
fig=figure();
set(fig, 'Position', [10 10 1500 700])
pcolor(mean(tas_ann_DMI_hist_tot,3))
shading flat
hold on;
contour(flip_lat_DMI, 'k','ShowText','on');
contour(flip_lon_DMI, 'k','ShowText','on');
caxis([min_pr max_pr]);colormap(cmap);
axesHandles = findobj(get(fig,'Children'), 'flat','Type','axes');
axis(axesHandles,'square')
set(gca,'YTick',[]);
set(gca,'XTick',[]);
I found a solution.
flip_lon_DMI(flip_lon_DMI<-160) = NaN;
flip_lon_DMI(flip_lon_DMI>190) = NaN;
...
contour(flip_lat_DMI,-90:5:-60,'k--','ShowText','on','Color',[.5 .5 .5]);
contour(flip_lon_DMI,-150:30:180,'k--','ShowText','on','Color',[.5 .5 .5]);
I am tring to increment power of A matrix in for loop but getting error
"input must be a scalar or square matrix"
function [S] = myst()
st_1 =[0.1490 0 0.1723 0.1786 0.2015 0.1387 0.1600]';
for A = [0.5 0 0 0 0 0.5 0.2;0 0 0 0 0 0 0 ; 0 0 0 0.4 0.4 0 0;0 0 1 0.2 0.2 0 0;0 0 0 0.4 0.2 0 0.4;0 0 0 0 0 0.5 0;0.5 0 0 0 0.2 0 0.4]
for n = 1:1:77
A_n = A^n; % this A matrix is changing its index after every loop,
%i don't know why index of A_n is changing?
S = (A_n)*st_1;
if S(1,1) == st_1(1,1)
disp(n);
break
end
end
end
disp(A);
end
When writing a for loop like for A=[1,2,3;4,5,6;7,8,9],disp(A);end you are iterating the individual column of A. Just remove the outer for loop when you intend to process the full matrix:
function [S] = myst()
st_1 =[0.1490 0 0.1723 0.1786 0.2015 0.1387 0.1600]';
A = [0.5 0 0 0 0 0.5 0.2;0 0 0 0 0 0 0 ; 0 0 0 0.4 0.4 0 0;0 0 1 0.2 0.2 0 0;0 0 0 0.4 0.2 0 0.4;0 0 0 0 0 0.5 0;0.5 0 0 0 0.2 0 0.4]
for n = 1:1:77
A_n = A^n; % this A matrix is changing its index after every loop,
%i don't know why index of A_n is changing?
S = (A_n)*st_1;
if S(1,1) == st_1(1,1)
disp(n);
break
end
end
disp(A);
end
I was searching for the implementation of Perona & Malik filter in Matlab, when i found this link "the link has an implementation for Perona and Malik filter using Matlab"
but there is the matrices that i didn't understand what is the use of them:
% 2D convolution masks - finite differences.
hN = [0 1 0; 0 -1 0; 0 0 0];
hS = [0 0 0; 0 -1 0; 0 1 0];
hE = [0 0 0; 0 -1 1; 0 0 0];
hW = [0 0 0; 1 -1 0; 0 0 0];
hNE = [0 0 1; 0 -1 0; 0 0 0];
hSE = [0 0 0; 0 -1 0; 0 0 1];
hSW = [0 0 0; 0 -1 0; 1 0 0];
hNW = [1 0 0; 0 -1 0; 0 0 0];
any one has an idea what are these matrices, or what do they mean?
These are the kernels for getting the gradients in North, East, South and West directions, and also for dialgonals (NE = North East etc.).
hN = [0 1 0; 0 -1 0; 0 0 0];
or nicely formatted:
0 1 0
0 -1 0
0 0 0
Convolution of this kernel with an image yields the gradient in "North direction". Basically, this is a mask of factors that you apply to all your 3x3 areas of your image and since all except two entries are 0, what you get is the pixel at the top minus that in the center, thus the gradient in y-direction.
I am using Matlab and Euler Angles in order to reorient a 3axes coordinate system. Specifically,
Rz = [cos(ψ) sin(ψ) 0;-sin(ψ) cos(ψ) 0;0 0 1];
Ry = [cos(φ) 0 -sin(φ);0 1 0;sin(φ) 0 cos(φ)];
Rx = [1 0 0;0 cos(θ) -sin(θ);0 sin(θ) cos(θ)];
Rtotal = Rz*Ry*Rz
Then I loop through my old system coordinates (x,y,z) and make a vector coord_old. Then I get the reoriented system with (xn,yn,zn)
for i=1:size(num,1)
coord_old = [x(i,1);y(i,1);z(i,1)];
coord_new = Rtotal*coord_old;
xn(i,1) = coord_new(1,1);
yn(i,1) = coord_new(2,1);
zn(i,1) = coord_new(3,1);
end
My issue is that when θ,φ,ψ≃0 then x->-y and y->x and when θ,φ≃0 and ψ=90 then x and y will not rotate! That means that when x,y should rotate they don't and when they shouldn't rotate they stay as they were!
--EDIT--
For example, when ψ=20.0871, φ=0.0580 and θ=0.0088 I get these results
See that x->-y and y->x while z doesn't change at all!
Any thoughts?
Ok, I see two main problems here:
Rtotal = Rz*Ry*Rz is probably not what you want since Rz is multiplied twice. I think you mean Rtotal = Rz*Ry*Rx.
Your rotation matrix seems to be incorrect. Check this Wikipedia artice to get the correct signs.
Here a corrected rotation matrix:
Rz = [cos(psi) -sin(psi) 0; sin(psi) cos(psi) 0; 0 0 1];
Ry = [cos(phi) 0 sin(phi); 0 1 0; -sin(phi) 0 cos(phi)];
Rx = [1 0 0; 0 cos(theta) -sin(theta); 0 sin(theta) cos(theta)];
Rtotal = Rz*Ry*Rx;
With this matrix I get the correct results:
x=1; y=2; z=3;
psi=0; phi=0; theta=0;
[xn,yn,zn] >> 1 2 3
x=1; y=2; z=3;
psi=90/180*pi; phi=0; theta=0;
[xn,yn,zn] >> -2 1 3
And here a full graphical example of a cube in 3d-space:
% Create cube (not in origin)
DVert = [0 0 0; 0 1 0; 1 1 0; 1 0 0 ; ...
0 0 1; 0 1 1; 1 1 1; 1 0 1];
DSide = [1 2 3 4; 2 6 7 3; 4 3 7 8; ...
1 5 8 4; 1 2 6 5; 5 6 7 8];
DCol = [0 0 1; 0 0.33 1; 0 0.66 1; ...
0 1 0.33; 0 1 0.66; 0 1 1];
% Rotation angles
psi = 20 /180*pi; % Z
phi = 45 /180*pi; % Y
theta = 0 /180*pi; % X
% Rotation matrix
Rz = [cos(psi) -sin(psi) 0; sin(psi) cos(psi) 0; 0 0 1];
Ry = [cos(phi) 0 sin(phi); 0 1 0; -sin(phi) 0 cos(phi)];
Rx = [1 0 0; 0 cos(theta) -sin(theta); 0 sin(theta) cos(theta)];
Rtotal = Rz*Ry*Rz;
% Apply rotation
DVertNew = Rtotal * DVert';
% Plot cubes
figure;
patch('Faces',DSide,'Vertices',DVert,'FaceColor','flat','FaceVertexCData',DCol);
patch('Faces',DSide,'Vertices',DVertNew','FaceColor','flat','FaceVertexCData',DCol);
% Customize view
grid on;
axis equal;
view(30,30);
When I use your code and insert 0 for all angles, I get Rtotal:
Rtotal =
1 0 0
0 1 0
0 0 1
This is the identity matrix and will not change your values.
You have an error in your matrix multiplication. I think you should multiply: Rtotal*coord_old. I think you are missing the _old. depending on what is in you coordvariable, this may be the bug.
When I run:
for i=1:size(1,1)
coord_old = [1;2;3];
coord_new = Rtotal*coord_old;
xn(i,1) = coord_new(1,1);
yn(i,1) = coord_new(2,1);
zn(i,1) = coord_new(3,1);
end
I get the correct result:
coord_new =
1
2
3
Thank you both #Steffen and #Matt. Unfortunately, my reputation is not high enough to vote Up your answers!
The problem was not with Rtotal as #Matt correctly stated. It should be as it was Rz*Ry*Rx. However, both your ideas helped me test my code with simple examples (5 sets of coordinates and right hand rule), and realize where my (amateur) mistake was.
I had forgotten I had erased parts of codes where I was expressing my angles to degrees... I should be using sind & cosd instead of sin and cos.