Indexing Problem with a For Loop

조회 수: 3 (최근 30일)
Tristen Hernandez
Tristen Hernandez 2019년 12월 13일
답변: Tristen Hernandez 2019년 12월 13일
Hi, I'm trying to create a function that with given inputs gives an output of various things that will then be taken by another function to plot it. However, as it stands right now I'm having a problem with the calculations and for-loop indexing. I've tried various things and I'm still stuck. The error I'm recieving right now is:
Index in position 2 is invalid. Array indices must be positive integers
or logical values.
Error in calculation (line 58)
Upeak(count+1,2*eta(i-1))= t(j); %the current value of
time
Error in main (line 27)
[t,S,i_arr,Upeak,Lpeak]=calculation(m,time,tsd,k,eta);
The project has 3 parts with this being the major function that will be taken in by a plotting function. The code I'm currently working on is this:
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=2:length(eta)% Run index 'i' from 1 to the length of 'eta' array
count = 1; % Initialize both counters to 1
count1 = 1; %
if eta(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 eta(i)];
end
wd = w0.*(sqrt(1-eta(i).^2)) ;% Fill in for wd
%%
for j=2:length(t)
S(i,j) = exp((-eta(i)).*w0.*t(j)).*(A.*cos(wd.*t(j))) ;
if eta(i) < 1 && t(j) > 3
right = S(i,j)
middle = S(i,j-1)
left = S(i,j-2)
if middle > left && middle > right% Middle value is greater than both left AND right
Upeak(count+1,2*eta(i-1))= t(j); %the current value of time
Upeak(count+1,2*eta(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
Lpeak(count+1,2*(i-1)) = t(j) ;
Lpeak(count+1,2*(i))=S(i,j-1) ;
count1= count1 + 1;
end
end
end
end
%%
if i == length(eta2) % 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.3d \n', Upeak());
else
% Output a table, with proper headings, of the "Lpeak" array
fprintf(' Lpeak Data Points \n')
fprintf('%3.3d \n', Lpeak());
end
end
Earlier I was able to get it to run without error, however my plots were all empty and I didn't get my tables with the data printed either. So by tweaking a few things I've arrived at my current scenarior and honestly have no idea what to do.

답변 (1개)

Tristen Hernandez
Tristen Hernandez 2019년 12월 13일
I was just able to get it to run by transforming:
Upeak(count+1,2*eta(i-1))= t(j); %the current value of time
Upeak(count+1,2*eta(i))=S(i,j-1) ; %S(<current i>, <current j - 1>)
count=count + 1 ; %add 1 to count
into this:
Upeak(count+1,2*i-1)= t(j); %the current value of time
Upeak(count+1,2*i)=S(i,j-1) ; %S(<current i>, <current j ->)
count=count + 1 ; %add 1 to count
However, it's still not plotting any of the data

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by