Asked by Serena Solanki
on 20 Feb 2018

Hey All hope you can help

I am having trouble getting data stored from my initial loop which I want to use in a later loop. The initial loop produces 40 sets of unique 2*2 matrix (A).

I am also struggling with being able to call the Matrix (A) in my nested loop i.e. I want to call the entire matrix and not just values within the matrix. My internal loop within the nested matrix works I just need the outer loop to work.

for example- A1= [0 1; 1.5 4] and A2= [0 1; 1 5.6] etc and i want to be able to call A1* .... etc A2 *... etc

Thank you in advance!

zita =0.05; %inherrent damping

m= 773; %mass in MG

b=[0

1/m];

for T0=0.1:0.1:4; % Range of Time Periods defined in seconds

w = (2*pi)/T0; %rad/s

w2=w*w;

zita=0.05;

A=[0 1

-w2 -(2*zita*w)];

end

%nested loop to calculate the displacements with the corresponding A matrix

for % I need to call the A matrix here

for i=1:(NPTS_interp-1)% Runge-Kutta loop, i = incremental time step

g1 = A*yt(:,i)+b*ft_interp(i);

g2 = A(1:end)*(yt(:,i)+ 0.5*g1*dt_interp)+0.5*b*(ft_interp(i)+ft_interp(i+1));

g3 = A(1:end)*(yt(:,i)+ 0.5*g2*dt_interp)+0.5*b*(ft_interp(i)+ft_interp(i+1));

g4 = A(1:end)*(yt(:,i)+g3*dt_interp)+b*ft_interp(i+1);

ynew = yt(:,i)+(g1+2*g2+2*g3+g4)*dt_interp/6;

yt(:,i+1)=ynew; %storage

ut=yt(1,:); %displacement values

vt=yt(2,:);%velocity values

[pks,locs]=findpeaks(ut,t_interp);

Max_displacement=max(pks);

end

end

Answer by Bob Nbob
on 20 Feb 2018

In order to store the initial data I would suggest using a cell array.

A(T0) = [your array here];

Then when you call the specific cell you want in your nested loop you can call the entire array as a variable.

for I = 1:length(A) %Length may not be the best measurement here

for ....

g1 = A{I}*yt(:,i)+....

end

end

Obviously my variable convention needs some work, but I think you get the idea. You may need to index the array you are calling as A(I) rather than A{I}, as A(I) is the cell itself, while A{I} is the contents of the cell.

Serena Solanki
on 21 Feb 2018

Hi Bob Yes that is correct i want to call A for when T0=0.1 etc

I think i have managed to sort the loop out, but i am still struggling with the indexing so that i can call out the first matrix for A.

so i have tried doing y=A(1:4)

this returns all of the values of A but as a 1*4 matrix which is not what i want. How do i retain the 2*2 matrix of A? or just call the first 4 sets of values

e.g. when i use y=A(1:4)

it returns

y= 0 1 0.5 7

y= 0 1 0.7 9

y= etc

How can i then call the first 4 values and then repeat for the next set?

Hope that makes sense and again thank you very much for helping me.

for index=1:T0(2);

i=1:(NPTS_interp);

w=(2*pi)/t(index);

w2=w*w;

zita=0.05;

m=773;

A=[0 1

-w2 -(2*zita*w)];

y=A(1:4)

Bob Nbob
on 21 Feb 2018

Indexing is generally pretty specific to what type of variable you are looking at. Vector arrays are pretty simple and consistent, but be careful when cells or structures become involved.

The reason A is coming out as a row vector is because you don't specify a new row beginning with ';'. If you want A to be 2x2 try:

A = [0 1;

-w2 -(2*zita*w)]; % I'm being lazy with the white space

If you want to store the entire A matrix for each iteration then you need to make A a cell array where each cell contains the 2x2 matrix. For this you still only need one index, but that index requires curly braces {} as you want to specify the contents of the cell.

A{index} = [your array here];

If you ever want to call a specific value out of one of those stored matrices, you need a second index. The first calls the cell, while the second calls the specific element of the array.

A{index}(1,2) = 1;

This can be used to define specific values as well.

If you want to have A be a single array matrix that grows with each index I would suggest making individual rows, like you have now. Simply index the specific row of A and then enter the matrix values. Then when you want to use them again, you just recall that specific row with the same indexing.

A(index,:) = [0 1 -w2 -(2*zita*w)]; % Where 'index' indicates row, and ':' indicates all columns.

y = A(index,:); % Sets 'y' to just row 'index' of A matrix

Serena Solanki
on 21 Feb 2018

Hi Bob

Thank you for helping me with this, it has really helped! I have tried experimenting and setting A1=A(:,:) and using your indexing has also worked too. I just need to sort out my loop.

Many thanks again

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.