Azzera filtri
Azzera filtri

Storing multiple matrices from a loop into a single variable without cell function

5 visualizzazioni (ultimi 30 giorni)
Hello guys I need help in storing the A matrix for the loop at each iteration. I want to know if I can store this A matrix without using the cell function. Here is the code:
clear
clc
clear all
n = 26;
q1(1) = 0.002*n;
q2(1) = 0.001*n;
q3(1) = 0.005*n;
q4(1) = sqrt(1-(q1(1)).^2-(q2(1)).^2-(q3(1)).^2);
wx(1) = 0.0002+0.0001*n;
wy(1) = 0.0003+0.0001*n;
wz(1) = 0.0004+0.0001*n;
jx = 2.1*10^-3;
jy = 2*10^-3;
jz = 1.9*10^-3;
delta_t = 0.1;
Nt = 3.6*10^-10;
t(1) = 0;
phi(1) = atan((2*(q2(1)*q3(1)+q1(1)*q4(1)))/(1-2*(q1(1).^2+q2(1).^2)));
theta(1) = asin(2*(q4(1)*q2(1)-q1(1)*q3(1)));
ksi(1) = atan((2*(q4(1)*q3(1)+q1(1)*q2(1)))/(1-2*(q2(1).^2+q3(1).^2)));
i = 1;
n_iteration = 54000;
while i<= n_iteration
t(i+1)= t(i)+delta_t;
wx(i+1) = wx(i)+(delta_t/jx)*(jy-jz)*wz(i)*wy(i)+(delta_t/jx)*Nt;
wy(i+1) = wy(i)+(delta_t/jy)*(jz-jx)*wx(i)*wz(i)+(delta_t/jy)*Nt;
wz(i+1) = wz(i)+(delta_t/jz)*(jx-jy)*wx(i)*wy(i)+(delta_t/jz)*Nt;
q1(i+1) = q1(i)-0.5*delta_t*(q2(i)*wx(i)+q3(i)*wy(i)+q4(i)*wz(i));
q2(i+1) = q2(i)+0.5*delta_t*(q1(i)*wx(i)-q4(i)*wy(i)+q3(i)*wz(i));
q3(i+1) = q3(i)+0.5*delta_t*(q4(i)*wx(i)+q1(i)*wy(i)-q2(i)*wz(i));
q4(i+1) = q4(i)-0.5*delta_t*(q3(i)*wx(i)-q2(i)*wy(i)-q1(i)*wz(i));
phi(i) = atan((2*(q2(i)*q3(i)+q1(i)*q4(i)))/(1-2*(q1(i).^2+q2(i).^2)));
theta(i) = asin(2*(q4(i)*q2(i)-q1(i)*q3(i)));
ksi(i) = atan((2*(q4(i)*q3(i)+q1(i)*q2(i)))/(1-2*(q2(i).^2+q3(i).^2)));
A1_1(i) = cos(theta(i)).*cos(ksi(i));
A1_2(i) = cos(theta(i)).*sin(ksi(i));
A1_3(i) = -sin(theta(i));
A2_1(i) = -cos(phi(i)).*sin(ksi(i))+sin(phi(i)).*sin(theta(i)).*cos(ksi(i));
A2_2(i) = cos(phi(i)).*cos(ksi(i))+sin(phi(i)).*sin(theta(i)).*sin(ksi(i));
A2_3(i) = sin(phi(i)).*cos(theta(i));
A3_1(i) = sin(phi(i)).*sin(ksi(i))+cos(phi(i)).*sin(theta(i)).*cos(ksi(i));
A3_2(i) = -sin(phi(i)).*cos(ksi(i))+cos(phi(i)).*sin(theta(i)).*sin(ksi(i));
A3_3(i) = cos(phi(i)).*cos(theta(i));
i=i+1
end

Risposte (1)

Chendi Lin
Chendi Lin il 9 Apr 2021
Hi Ilker,
If I understand your question correctly, A is a 3x3 matrix. For each i in the iteration, you want to store A without using cell.
Have you tried to store everything in a 3D matrix? For example,
As = zeros(n_iteration,3,3);
while i<= n_iteration
As[i,:,:] = A;
end
Best,
CD
  7 Commenti
Ilker Enes Çirkin
Ilker Enes Çirkin il 9 Apr 2021
It did give me an answer but I wasn't satisfied with it. I can share the answer that worked better for me as:
I appriciate your help. Thank you.
A(3*i+1:3*i+3,1:3) = [cos(theta(i)).*cos(ksi(i)) cos(theta(i)).*sin(ksi(i)) -sin(theta(i));
-cos(phi(i)).*sin(ksi(i))+sin(phi(i)).*sin(theta(i)).*cos(ksi(i)) cos(phi(i)).*cos(ksi(i))+sin(phi(i)).*sin(theta(i)).*sin(ksi(i)) sin(phi(i)).*cos(theta(i));
sin(phi(i)).*sin(ksi(i))+cos(phi(i)).*sin(theta(i)).*cos(ksi(i)) -sin(phi(i)).*cos(ksi(i))+cos(phi(i)).*sin(theta(i)).*sin(ksi(i)) cos(phi(i)).*cos(theta(i));];
Walter Roberson
Walter Roberson il 9 Apr 2021
I think you will find that the code works much better if you use 3D indexing such as A(i,:,:) = [that array]
You can reshape() or permute() afterwards .

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Prodotti


Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by