Updating element defined as a variable in a matrix for a given range

2 views (last 30 days)
Hello,
I created a gauss elimination with partial pivoting and back substitution to solve a system of linear equations. What I am having trouble with is creating a matrix that has an element defined as a variable that is updated for a given range of numbers in order to produce a number of solutions based on the range and increments provided. I recieve the following error message:"Error using vertcat: Dimensions of arrays being concatenated are not consistent."
Thank you for your help.
function [x] = FwdElimPPivBckSub(A,b)
tic
R3 = (30.1:.1:100);
A = [1 -1 -1 0 0;
0 0 1 -1 -1;
30 0 0 -25 -25;
-25 0 R3 0 -6;
0 0 -6 0 21;];
b = [0; 0; 110; 0; -45];
n = length(b);
for k = 1:n-1
p = k;
big = abs(A(k,k));
for ii = k+1:n
dummy = abs(A(ii,k));
if dummy > big
p = ii;
end
end
if p ~= k
for jj = k:n
dummy = A(p,jj);
A(p,jj)= A(k,jj);
A(k,jj)= dummy;
end
dummy = b(p);
b(p) = b(k);
b(k) = dummy;
end
for i = k+1:n
factor = A(i,k)/A(k,k);
for j = k+1:n
A(i,j) = A(i,j)-factor *A(k,j);
end
b(i) = b(i) - factor*b(k);
end
end
for i = 1:n
x(i) = b(i)/A(i,i);
end
for i = n-1:-1:1
sum = b(i);
for j = i+1:n
sum = sum - A(i,j)*x(j);
end
x(i) = sum/A(i,i);
end
toc
  2 Comments
AbeSal
AbeSal on 16 Nov 2019
Hi Walter,
Thank you for your feedback. What I am trying to define is an array that will in turn produce a list of numbers from 30.1 to 100 in lets say increments of 1 that individually define R3 and create unique solutions when the system of linear equations is solved.
-Abner
Walter Roberson
Walter Roberson on 16 Nov 2019
R3_vals = 30.1:.1:100;
num_R3 = length(R3_vals);
results_cell = cell(1, num_R3);
b = [0; 0; 110; 0; -45];
for R3_idx = 1 : num_R3
R3 = R3_vals(R3_idx);
A = [1 -1 -1 0 0;
0 0 1 -1 -1;
30 0 0 -25 -25;
-25 0 R3 0 -6;
0 0 -6 0 21;];
results_cell{R3_idx} = reshape(FwdElimPPivBckSub(A,b), [], 1);
end
all_results = cell2mat(results_cell);

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 15 Nov 2019
R3 = (30.1:.1:100);
R3 is now 1 x 700
A = [1 -1 -1 0 0;
0 0 1 -1 -1;
30 0 0 -25 -25;
first row, second row, third are all 1 x 5
-25 0 R3 0 -6;
There is an R3 in the middle there, and it is 1 x 700, so this line is 1 x (2 + 700 + 2) which is 1 x 704
0 0 -6 0 21;];
and that line is 1 x 5.
What I am having trouble with is creating a matrix that has an element defined as a variable that is updated for a given range of numbers in order to produce a number of solutions based on the range and increments provided.
You pass in A and b. You should not be defining them inside the function. You should be calling the function in a loop, passing in different variations of A, and doing something appropriate with the results returned by the function.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!