Trouble Creating a Vector using a for loop with matrices
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I'm having trouble creating a vector of answers with a for loop involving matrices. When I call on w, it gives me different values than if I were to call on a specific part of w.
B1=[1 0; 0 -7.5];
D1=[0 0;0 10];
X1=[0 0;0 -2.5];
B2=[49.8 0;-0.127 -50];
D2=[25 0;0 25];
A2=[25 0;0 25];
B3=[49.8 0;-0.127 -50];
D3=[25 0;0 25];
A3=[25 0;0 25];
B4=[49.8 0;-0.127 -50];
D4=[25 0;0 25];
A4=[25 0;0 25];
B5=[7.5 0;0 1];
A5=[-10 0;0 0];
Y5=[2.5 0;0 0];
o=[0 0;0 0];
%M
M=[B1 D1 X1 o o;A2 B2 D2 o o;o A3 B3 D3 o;o o A4 B4 D4;o o Y5 A5 B5];
Mi=inv(M);
G=[.21;0;0;0;0;0;0;0;0;0.127];
CG=[.1;.1;.1;.1;.1;.1;.1;.1;.1;.1];
%find L
%L=[b1 o o o o;a2 b2 o o o;o a3 b3 o o;o o a4 b4 o;o o y5 a5 b5]
%U=[I -E1 -x1 o o;o I -E2 o o;o o I -E3 o;o o o I -E4;o o o o I]
n=2;
NJ=5;
w=zeros(2*NJ,1);
for j=1
L(1:(j+1),1:(j+1))=B1; %b(1)=B1
U(1:j+1,j+2:j+3)=-inv(L(1:j+1,1:j+1))*D1; %E(1)=D1*inv(-b(1))
U(1:j+1,2*j+3:2*j+4)=-inv(L(1:j+1,1:j+1))*X1; %x(1)=X1*inv(-b(1))
w(1:2)=inv(L(1:j+1,1:j+1))*G(1:2); %ksi(1) = b(1)^-1 * G(1)
end
for j=2:NJ-1
L(2*j-1:2*j,2*j-3:2*j-2)=A2; %a(2)=A2
L(2*j-1:2*j,2*j-1:2*j)=B2+L(2*j-1:2*j,2*j-3:2*j-2)*U(2*(j-1)-1:2*(j-1),2*(j-1)+1:2*(j-1)+2); %b(2)=B2+a(2)*E(1)
U(2*j-1:2*j,2*j+1:2*j+2)=-inv(L(2*j-1:2*j,2*j-1:2*j))*D2; %E(2)=-inv(b(2))*D(2)
w(2*j-1:2*j)=-inv(L(2*j-1:2*j,2*j-1:2*j))*((-G(2*j-1:2*j)+(L(2*j-1:2*j,2*j-3:2*j-2)*w((2*(j-1)-1):(2*(j-1)))))); %ksi(2)=inv(-b(2))*(-G(2)+a(2)*ksi(1))
end
for j=NJ
L(2*j-1:2*j,2*j-5:2*j-4)=Y5; %y(5)=Y5
L(2*j-1:2*j,2*j-3:2*j-2)=A5+L(2*j-1:2*j,2*j-5:2*j-4)*U(2*(j-2)-1:2*(j-2),2*(j-2)+1:2*(j-2)+2); %a(5)=A5+y(5)*E(NJ-2)
L(2*j-1:2*j,2*j-1:2*j)=B5+L(2*j-1:2*j,2*j-3:2*j-2)*U(2*(j-1)-1:2*(j-1),2*(j-1)+1:2*(j-1)+2); %b(5)=B5+a(5)*E(4)
w(2*j-1:2*j)=-inv(L(2*j-1:2*j,2*j-1:2*j))*(-G(2*j-1:2*j)+L(2*j-1:2*j,2*j-3:2*j-2)*w(2*(j-1)-1:2*(j-1))+L(2*j-1:2*j,2*j-5:2*j-4)*w(2*(j-2)-1:2*(j-2)))%-inv(b(5))*(-G(5)+a(5)*w(4)+y*(w(3))
end
%w=inv(L)*G
%C=inv(U)*w
1 commentaire
Stephen23
le 7 Nov 2019
Note: to make your code more efficient and robust you should probably be using mldivide instead of inv and *. Explicit matrix inversion is rarely required (because there are better methods for solving such systems of equations).
Réponses (0)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!