- Initialize "A0" with 10 different values.
- Initialize an empty array "final_values" to store results of all 10 iterations.
- Use a "for" loop to iterate through all 10 values of "A0" and create a new "eqn1" equation for each value of "A0".
- Store the result of each iteration in the "final_values" array.
System of linear equations with for loop
조회 수: 16 (최근 30일)
이전 댓글 표시
Hello, is it possible to solve a system of linear equations with a for loop? I would like to iterate one of the known parameters and see all of the solutions of the system for each iteration. Here is an example of how I am solving the system without the iteration:
A0 = 1;
B0 = 2;
C0 = 3;
syms a b c
eqn1 = a - b == A0;
eqn2 = a + b + c == B0;
eqn3 = a - b + c == C0;
[A,B] = equationsToMatrix([eqn1, eqn2, eqn3], [a b c]);
X = linsolve(A,B);
a = double(X(1))
b = double(X(2))
c = double(X(3))
Now, what if the values of A0 are composed of a vector of 10 different values? I should have 10 values for each of the a, b, c parameters as output. I was thinking to iterate the equations with a for loop. How can I do that?
Thank you all in advance!
댓글 수: 0
답변 (1개)
Vishesh
2023년 9월 20일
I understand that you want to solve a system of linear equations using a "for" loop for 10 different values of "A0" in your script.
You can achieve this using the following steps:
The script for the above steps is provided below:
A0 = [1 2 3 4 5 6 7 8 9 10]; %taking 10 different values of A0.
B0 = 2;
C0 = 3;
syms a b c;
eqn2 = a + b + c == B0;
eqn3 = a - b + c == C0;
final_values=[]; %initializing an empty array to store values of a,b and c in each iteration.
for i=1:10 % iterating each value of "A0"
eqn1 = a - b == A0(i);
[A,B] = equationsToMatrix([eqn1, eqn2, eqn3], [a b c]);
X = linsolve(A,B);
final_values=[final_values;[double(X(1)) double(X(2)) double(X(3))]]; % storing values of a,b and c.
end
disp(final_values);
Please refer to the following documentation for more information on the "for" loop:
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Particle & Nuclear Physics에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!