for loop with ode45 function
Mostra commenti meno recenti
Hi,
I am trying to vary one of the parameters of my system. Then, I would like to input this value into the ode45 function, and determine the maximum displacement of my system, such that it is recorded in a variable, max_displ. I would like to iterate for many different values of my parameter, k_l, so I thought to use a for loop.
dk=1;
k_l=[1:dk:2]; %0 to 2 is just a test
for ii=1:dk:numel(k_l) %will iterate for all the values of the k_l from 0 up to 2
k_l=k_l(ii)
[t,z] = ode45(@(t, z) f(t,z,k_l,disp_data,vel_data,t_data),T,IC);
disp_data_int=interp1(t_data,disp_data,t,'spline'); %extracting the displacement data from the earthquake
displ=(z(:,8)); %extracting the displacement data for a particular stiffness value
disp_a=abs(disp_data_int-displ); %finding absolute value of diffence between displacment of earthquake and building
max_displ(ii)=max(disp_a) %finding the maximumm displacement for a particular stiffness value
end
figure;
hold on
plot(k_l,max_displ);
MATLAB indicates this error 'Index exceeds the number of array elements (1)', for this line of code,
k_l=k_l(ii)
Any suggestions would be greatly appreciated.
Thanks,
Kostas
Risposta accettata
Più risposte (1)
Torsten
il 31 Mar 2022
Look at what your loop does with the array k_l.
k_l is set to [1 2] before the loop.
For ii=1, the command
k_l = k_l(1)
sets k_l to a scalar, namely k_l(1) = 1.
For ii=1,
k_l = k_l(2)
tries to access the 2nd element from k_l.
But k_l is a scalar, thus has no second element. That's why MATLAB errors.
Workaround:
Replace
k_l = k_l(ii)
by
k_lii = k_l(ii)
1 Commento
Kostas Kalabokas
il 31 Mar 2022
Categorie
Scopri di più su Ordinary Differential Equations in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!