Matrix Dimensions Must agree

4 Ansichten (letzte 30 Tage)
Tristen Hernandez
Tristen Hernandez am 12 Dez. 2019
Kommentiert: Walter Roberson am 13 Dez. 2019
Hi, currently I'm trying to create a function that with the given inputs will output various graphs of the system, however I'm running into problem with the matrix dimensions on line 49 not working (the S function). I was wondering if a fresh set of eyes could help me see where I'm going wrong with it. Also note this is a function that is to be used in another script with a plotting function. Any and all help very much appreciated.
function [t,S,i_arr,Upeak,Lpeak] =calculation(m,time,tsd,k,eta)
%%%%%%%%%% Write 4-5 lines (comments about what this function does and sends back)
%%%%%%%%%%
%%%%%%%%%%
%%%%%%%%%%
% This function needs to take the following as input values:
% (1) m (mass)
% (2) time
% (3) tsd
% (4) k (stiffness)
% (5) eta
tsd = 0.01;
m = 2.5;
k = 175; %stiffness
time = [0 4];
eta = [0.015 0.09 1]
eta2 = [0.02 1 0.08 2 1.5 0];
t=(0:tsd:4);
A = 5;
w0= sqrt(k/m) ; % Determine w0
% Build three empty arrays
i_arr=[]; % Saves all etas less than 1
Upeak=[]; % Saves all upper peaks
Lpeak=[]; % Saves all lower peaks
%%
for i=1:length(eta2) % Run index 'i' from 1 to the length of 'eta' array
count = 1; % Initialize both counters to 1
count1 = 1; %
if i < 1 %check if the current iteration (i) of eta is less than 1
% Set first row and the 2*i-1 column for both Upeak and Lpeak to 0
Upeak(1,2*i-1)=0;
Lpeak(1,2*i-1)=0;
Upeak(1,2*i)=A;
Lpeak(1,2*i)=-A;
% Add this iteration of eta to i_arr
i_arr=[i_arr i];
end
wd = w0.*(sqrt(1-eta.^2)) ;% Fill in for wd
%%
for j=1:length(t)
S(i,j) = exp((-i).*w0.*t).*(A.*cos(wd.*j)) ;
if i > 1 && j > 3
right = S(i,j)==exp(-i*w0.*t)*A*cos(wd.*j);
middle = S(i,j)==exp(-i*w0.*t)*A*cos(wd.*(j-1));
left =S(i,j)==exp(-i*w0.*t)*A*cos(wd.*(j-2));
%%
if middle > left && middle > right% Middle value is greater than both left AND right
Upeak(count+1,2*i-1)= j; %the current value of time
Upeak(count+1,2*i)=S(i,j-1) ; %S(<current i>, <current j - 1>)
count=count + 1 ; %add 1 to count
end
% Check if this is lower peak value
if middle < left && middle < right % Middle value is less than both left AND right
Lpeak(count+1,2*i-1)=j ; %the current value of time
Lpeak(count+1,2*i)=S(i,j-1) ; %S(<current i>, <current j - 1>)
count1= count1 + 1; %add 1 to count1
end
end
end
end
%%
if i == length(eta) % Check if the length of eta is 3 (for part (a))
% Output a table, with proper headings, of the "Upeak" array
fprintf(' Upeak Data Points \n')
fprintf('%3.3f \n', Upeak(i,j));
else
% Output a table, with proper headings, of the "Lpeak" array
fprintf(' Lpeak Data Points \n')
fprintf('%3.3f \n', Lpeak(i,j));
end
end

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 12 Dez. 2019
t=(0:tsd:4);
t is a vector
for j=1:length(t)
taking length(t) again implies t is a vector.
S(i,j) = exp((-i).*w0.*t).*(A.*cos(wd.*j)) ;
t is a vector and you use all of t, so the right hand side is a vector.
Considering that you loop j to length(t) you should probably be using t(j)

Weitere Antworten (1)

Tristen Hernandez
Tristen Hernandez am 12 Dez. 2019
S(i,j) = exp((-eta(i)).*w0.*t(j)).*(A.*cos(wd.*t(j))) ;
My S looks like this now and I'm getting the error of:
Unable to perform assignment because the indices on the left side are not
compatible with the size of the right side.
Error in calculation (line 49)
S(i,j) = exp((-eta(i)).*w0.*t(j)).*(A.*cos(wd.*t(j))) ;
Which I'm presuming probably has to deal with how I'm mulitplying the various elements?
  1 Kommentar
Walter Roberson
Walter Roberson am 13 Dez. 2019
eta = [0.015 0.09 1]
eta is a vector
wd = w0.*(sqrt(1-eta.^2)) ;% Fill in for wd
so wd is a vector the same size as eta. (Note: it is being calculated inside a loop even though w0 and eta are not changed inside the loop.)
S(i,j) = exp((-eta(i)).*w0.*t(j)).*(A.*cos(wd.*t(j))) ;
wd is a vector so the right hand side is a vector.
Since wd is the same size as eta, perhaps you should be indexing wd by the same thing you index eta by.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by