When I run this code:
pts= [-1 0; 0 1; 1 0; 0 -1];
xCenter = 0;
yCenter = 0;
plot(xCenter,yCenter,pts(:,1), pts(:,2),'g');
I get this plot:
What do I need to change so I will plot the + sign, that all the lines will start from the center.
Just add the origin between every pair of points
pts= [-1 0; 0 0; 0 1; 0 0; 1 0; 0 0; 0 -1];
otherwise you're skipping between the end-points of the "+", which is why you get 3 sides of a square.
You could do this automatically with something like
pts= [-1 0; 0 1; 1 0; 0 -1];
xCenter = 0;
yCenter = 0;
% Initialise the array to all be the center point
newPts = repmat( [xCenter, yCenter], size(pts,1)*2-1, 1 );
% Every other row comes from the original "pts"
newPts(1:2:2*size(pts,1),:) = pts;
Related
I seem to get the error "Warning: Matrix is singular to working precision." when trying to get delta_x. It should be using 5x1 and 5x5 matrices.
clc; close all; clear all;
phi = 1;
delta_x = 1;
error = 10e-15;
x = [ 0; 0; 0; 0; 0];
n =1;
B =0.025;
while norm(phi)>= error && norm(delta_x) >= error
G = [ 40e3 -20e3 -20e3 0 1; -20e3 20e3 0 0 0; -20e3 0 20e3 0 0; 0 0 0 0 0; 0 0 0 0 0];
fx = [ 0;
B*((-x(4)-0.7)*(x(2)-x(4))-(((x(2)-x(4))^2)/2));
B*((-x(4)-0.7)*(x(3)-x(4))-(((x(3)-x(4))^2)/2));
-B*((-x(4)-0.7)*(x(2)-x(4))-(((x(2)-x(4))^2)/2))- B*((-x(4)-0.7)*(x(3)-x(4))-(((x(3)-x(4))^2)/2));
0];
b = [ 0; 0; 0; 200e-6; 2.5];
dfx = [ 0 0 0 0 0;
0 -B*(0.7+x(2)) 0 B*(0.7+x(4)) 0;
0 0 -B*(0.7+x(3)) B*(0.7+x(4)) 0;
0 B*(0.7+x(2)) B*(0.7+x(3)) -2*B*(0.7+x(2)) 0;
0 0 0 0 0];
phi = G*x + fx - b;
m = G + dfx;
delta_x = -m\phi;
x = x+delta_x;
norm_delta_x(n) = norm(delta_x);
norm_phi(n) = norm(phi);
n = n+1;
end
The dimensions of matrices 5x1 and 5x5 are fine, but what you are doing in the step delta_x = -m\phi is solving for an inverse of matrix m. Since m is a matrix that is singular (try running det(m) and you will get a zero), an inverse matrix does not exist. Matlab sees this and notifies you by telling you "Matrix is singular to working precision".
Suppose that you have a matrix with:
0 0 0 .... 0
A 0 0 .... 0
B 0 0 .... 0
C 0 0 .... 0
0 0 0 .... 0
D 0 0 .... 0
E 0 0 .... 0
If I want to get a new array with the output:
[A+B+C D+E]
how would you do it? Of course I can always do loops and check for 0 but I'd like to know if there is any other alternative.
Use cumsum to generate a vector of grouping values, and then accumarray to do the sums:
x = [0; 1; 2; 4; 0; 7; 3];
result = accumarray(cumsum(x==0) + (x(1)~=0), x);
gives
result =
7
10
The + (x(1)~=0) part is necessary if x may not start with a zero. This part ensures that for
x = [1; 2; 4; 0; 7; 3];
the result is the desired
result =
7
10
With the above approach, each zero starts a new group. So, for
x = [0; 1; 2; 4; 0; 7; 3; 0; 0; 5; 0];
the result is
result =
7
10
0
5
0
If you want each run of one or more zeros to start a new group: first collapse consecutive zeros in x, then apply the above:
x = [0; 1; 2; 4; 0; 7; 3; 0; 0; 5; 0];
ind = [true; x(2:end)~=0 | x(1:end-1)~=0]; % index to remove a zero if preceded by zero
t = x(ind);
result = accumarray(cumsum(t==0) + (x(1)~=0), t);
gives
result =
7
10
5
0
Hello everybody I have a very simple problem, I have too many data y, p and r. So I want to calculate it in a single code.
This an example of my code if I breakdown into separate code
y1=45
y2=56
y3=67
p1=34
p2=45
p3=56
r1=23
r2=34
r3=45
Ryaw1=[cosd(y1) -sind(y1) 0;
sind(y1) cosd(y1) 0;
0 0 1]
Rpitch1=[cosd(p1) 0 sind(p1);
0 1 0;
-sind(p1) 0 cos(p1)]
Rroll1=[1 0 0;
0 cosd(r1) -sind(r1);
0 sind(r1) cosd(r1)]
R1=Ryaw1*Rpitch1*Rroll1
Coordinate1=R1*X0
Ryaw2=[cosd(y2) -sind(y2) 0;
sind(y2) cosd(y2) 0;
0 0 1]
Rpitch2=[cosd(p2) 0 sind(p2);
0 1 0;
-sind(p2) 0 cos(p2)]
Rroll2=[1 0 0;
0 cosd(r2) -sind(r2);
0 sind(r2) cosd(r2)]
R2=Ryaw2*Rpitch2*Rroll2
Coordinate2=R2*X0
Ryaw3=[cosd(y3) -sind(y3) 0;
sind(y3) cosd(y3) 0;
0 0 1]
Rpitch3=[cosd(p3) 0 sind(p3);
0 1 0;
-sind(p3) 0 cos(p3)]
Rroll3=[1 0 0;
0 cosd(r3) -sind(r3);
0 sind(r3) cosd(r3)]
R3=Ryaw3*Rpitch3*Rroll3
Coordinate3=R3*X0
Coordinate=[Cooedinate1 Coordinate2 Coordinate3]
The goals is to find "Coordinate" (in matrix - combined from Coordinate1, Coordinate2, Coordinate3, .... ,Coordinate..) from every y, p and r data with the same "X0" as a single primary data for calculation.
Sorry for my bad english,
Thanks :)
Use vectors and matrices instead of individual scalars. These are indexed in almost the same way as you had before, i.e. y1 becomes y(1).
Then you can easily loop over the code 3 times and save the repetition.
See my commented code below.
% Define some X0. This should be a column vector.
X0 = [1; 2; 3];
% Make y,p,r into 3 element vectors
y = [45 56 67];
p = [34 45 56];
r = [23 34 45];
% Make R, Ryaw, Rpitch and Rroll 3x3x3 matrices
R = zeros(3,3,3);
Ryaw = zeros(3,3,3);
Rpitch = zeros(3,3,3);
Rroll = zeros(3,3,3);
% Make Coordinate a 3x3 matrix
Coordinate = zeros(3,3);
% Loop k from 1 to 3
% For each 3x3x3 matrix, the kth 3x3 matrix is equivalent to your Ryawk, Rpitchk, Rrollk, Rk
for k = 1:3
Ryaw(:,:,k) = [cosd(y(k)) -sind(y(k)) 0
sind(y(k)) cosd(y(k)) 0
0 0 1];
Rpitch(:,:,k)= [cosd(p(k)) 0 sind(p(k))
0 1 0
-sind(p(k)) 0 cos(p(k))];
Rroll(:,:,k) = [1 0 0
0 cosd(r(k)) -sind(r(k))
0 sind(r(k)) cosd(r(k))];
R(:,:,k) = Ryaw(:,:,k)*Rpitch(:,:,k)*Rroll(:,:,k);
Coordinate(:,k) = R(:,:,k)*X0;
end
disp(Coordinate)
I'm trying to plot a frequency characteristic equation using ezplot, but Matlab gives the following warning, "Contour not rendered for non-finite ZData". I have used this command to plot frequency equations previously but now I get a warning and the plot display is empty and it does not change the axis range as well. Can someone please help. Would be much appreciated.
Here's the code i'm using.
% Transfer Matrix for Case-I, thin rotor
clear all;
clc;
EI = 1626;
l = 0.15;
m = 0.44108;
It = 2.178*10^-4;
I_p = 2.205*10^-5;
Itr = 0.24;
I_pr = 0.479;
syms p n;
F = [1 l*1i l^2/(2*EI)*1i l^3/(6*EI);
0 1 l/EI -l^2/(2*EI)*1i;
0 0 1 -l*1i;
0 0 0 l];
P = [ 1 0 0 0;
0 1 0 0;
0 -It*p^2+I_p*n*p 1 0;
-m*p^2 0 0 1];
P_r = [1 0 0 0;
0 1 0 0;
0 -Itr*p^2+I_pr*n*p 1 0;
-m*p^2 0 0 1];
A = F*P*F*P*F*P*F;
B = P_r*F*P*F*P*F;
r = A(1,2)/A(1,4);
a12_p = 0;
a22_p = A(2,2)-r*A(2,4);
a32_p = A(3,2)-r*A(3,4);
a42_p = A(4,2)-r*A(4,4);
Ap(2,2) = a22_p;
Ap(3,2) = a32_p;
Ap(4,2) = a42_p;
Ap(4,4) = 1;
C = B*Ap;
M = [C(3,2) C(3,4);
C(4,2) C(4,4)];
sol = det(M);
ezplot(sol,[-2*10^10 2*10^10]);
The sol is displayed if u ask for it but the plot doesn't display.
Thanks in advance for ur help !! Much appreciated.
I am trying to take a gradient of an image using the Prewitt filter. Can you tell me if this approach is correct?
I = imread('image.jpg')
Gx = [-1 0 1; -1 0 1; -1 0 1];
Gy = [1 1 1; 0 0 0; 1 1 1];
D = conv2(conv2(I, Gx), Gy)
imshow(D)
Is that correct? Is there a cleaner way to do it (no need to call conv2 twice)? Is conv2(I, Gx) the same as conv2(Gx, I)? (i.e. commutative?)
Thanks.
Judging by my wikipedia-ing it looks like what you should do is:
I = imread('image.jpg')
Gx = [-1 0 1; -1 0 1; -1 0 1];
Gy = [1 1 1; 0 0 0; -1 -1 -1];
A = sqrt( conv2(I,Gx).^2 + conv2(I,Gy).^2 );
imshow(A);
Link to Wikipedia Article