Generate a loop with Guassian Elimination for multiple values of one variable

조회 수: 2 (최근 30일)
Josh
Josh 2016년 2월 12일
편집: Marc 2016년 2월 12일
I'm trying to generate a loop that solves a system of equations in the matrix form Ax=b that has a solution vector x that then populates a 15 column solution matrix using all 401 possible values for F1. I've transposed my solution vector x but it currently only populates as a 1x15 with the final value of F1 which is 200.
F1 = 0:0.5:200;
F2 = 55;
F3 = 35;
%input left side of equilibrium equations
A = [1 1 0 0 0 0 0 0 0 0 0 0 0 0 0;
1 0 0 (1/sqrt(2)) 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 1 (1/sqrt(2)) 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 (3/sqrt(13)) 1 0 0 0 0 0 0 0 0;
0 0 -1 0 1 (2/sqrt(13)) 0 0 0 0 0 0 0 0 0;
0 0 0 0 -1 0 0 1 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 (3/sqrt(13)) 1 0 0 0 0;
0 0 0 0 0 0 0 -1 0 -(2/sqrt(13)) 0 1 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 (1/sqrt(2)) -(2/sqrt(5)) 0;
0 0 0 0 0 0 0 0 0 0 -1 0 -(1/sqrt(2)) (1/sqrt(5)) 0;
0 0 0 0 0 -(2/sqrt(13)) 0 0 0 (2/sqrt(13)) 0 0 0 (2/sqrt(5)) -(2/sqrt(5));
0 0 0 0 0 -(3/sqrt(13)) 0 0 -1 -(3/sqrt(13)) 0 0 0 -(1/sqrt(5)) -(1/sqrt(5));
0 0 0 -(1/sqrt(2)) 0 0 0 0 0 0 0 0 0 0 (2/sqrt(5));
0 0 0 -(1/sqrt(2)) 0 0 -1 0 0 0 0 0 0 0 (1/sqrt(5))];
%setup loop to solve equilibrium equations
for n = 1:length(F1);
%input right side of equilibrium equations
b = [F1(n)+F2+F3;
0;
0;
F1(n);
0;
0;
F2;
F3;
0;
0;
0;
0;
0;
0;
0];
x = (A\b)';
end

답변 (1개)

Marc
Marc 2016년 2월 12일
편집: Marc 2016년 2월 12일
All you are missing is preallocation of your solution matrix outside the loop.
solnMat = zeros(length(F1),15);
Inside the loop you can put your x's into solnMat
solnMat(i,:) = x;
Should give you a big solution matrix with all 401 x's.

카테고리

Help CenterFile Exchange에서 Mathematics and Optimization에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by