Accepting multiple values for a function. I want my function to accept multiple values for beta

1 Ansicht (letzte 30 Tage)
function x = backsub(U,b)
%FORWARDSUB Solve a lower triangular linear system
%Input:
% U = Upper triangular matrix (n by n)
% b = right-hand side vector (n by 1)
% Output:
%Solution of Ux=b (n by 1 vector)
n = length(U);
x= zeros(n,1);
for i = 1:3
x(i) =( b(i) - U(i, 1:i-1)*x(1:i-1) ) / U(i,i);
end
end
alpha= 0.1; composition of specific matrix
beta= 1e1 (here lies the problem, i want the code to accept values of [10, 100, 1000 to 10^12])
U = eye(5)+ diag([-1 -1 -1 -1],1);
U(1,[4 5]) = [ alpha-beta, beta ];
x_exact = ones(5, 1);
b = [alpha;0;0;0;1];
x=backsub(U,b)
  2 Kommentare
Bruno Luong
Bruno Luong am 21 Sep. 2023
Bearbeitet: Bruno Luong am 21 Sep. 2023
Is it recursive function (you call backsub in backsub)? How beta change between two recursion? When the recusion stops?
Walter Roberson
Walter Roberson am 21 Sep. 2023
I am pretty sure it is not intended to be recursive -- I think they posted the function and then the script to drive the function.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Walter Roberson
Walter Roberson am 21 Sep. 2023
U(1,[4 5]) = [ alpha-beta, beta ];
when beta is a vector, then you have a problem: you need different 5 x 5 U matrices for each different value of beta. You are not operating on "the same U matrix but different beta values" each time: you are operating on different U matrices each time.
So you will need to either switch to 3D calculations, with U being 5 x 5 x length(beta), and appropriate adjustment for the backsub() function -- or else you will need to loop your code.
  3 Kommentare
Walter Roberson
Walter Roberson am 21 Sep. 2023
Bearbeitet: Walter Roberson am 22 Sep. 2023
Looping...
But I didn't fix any bugs in your code.
format short g
alpha= 0.1; %composition of specific matrix
betas = 10.^(1:12);
baseU = eye(5)+ diag([-1 -1 -1 -1],1);
for K = 1 : length(betas)
U = baseU;
beta = betas(K)
U(1,[4 5]) = [ alpha-beta, beta ]
x_exact = ones(5, 1);
b = [alpha;0;0;0;1];
x=backsub(U,b)
end
beta =
10
U = 5×5
1 -1 0 -9.9 10 0 1 -1 0 0 0 0 1 -1 0 0 0 0 1 -1 0 0 0 0 1
x = 5×1
0.1 0 0 0 0
beta =
100
U = 5×5
1 -1 0 -99.9 100 0 1 -1 0 0 0 0 1 -1 0 0 0 0 1 -1 0 0 0 0 1
x = 5×1
0.1 0 0 0 0
beta =
1000
U = 5×5
1.0e+00 * 1 -1 0 -999.9 1000 0 1 -1 0 0 0 0 1 -1 0 0 0 0 1 -1 0 0 0 0 1
x = 5×1
0.1 0 0 0 0
beta =
10000
U = 5×5
1.0e+00 * 1 -1 0 -9999.9 10000 0 1 -1 0 0 0 0 1 -1 0 0 0 0 1 -1 0 0 0 0 1
x = 5×1
0.1 0 0 0 0
beta =
100000
U = 5×5
1.0e+00 * 1 -1 0 -1e+05 1e+05 0 1 -1 0 0 0 0 1 -1 0 0 0 0 1 -1 0 0 0 0 1
x = 5×1
0.1 0 0 0 0
beta =
1000000
U = 5×5
1.0e+00 * 1 -1 0 -1e+06 1e+06 0 1 -1 0 0 0 0 1 -1 0 0 0 0 1 -1 0 0 0 0 1
x = 5×1
0.1 0 0 0 0
beta =
10000000
U = 5×5
1.0e+00 * 1 -1 0 -1e+07 1e+07 0 1 -1 0 0 0 0 1 -1 0 0 0 0 1 -1 0 0 0 0 1
x = 5×1
0.1 0 0 0 0
beta =
100000000
U = 5×5
1.0e+00 * 1 -1 0 -1e+08 1e+08 0 1 -1 0 0 0 0 1 -1 0 0 0 0 1 -1 0 0 0 0 1
x = 5×1
0.1 0 0 0 0
beta =
1e+09
U = 5×5
1.0e+00 * 1 -1 0 -1e+09 1e+09 0 1 -1 0 0 0 0 1 -1 0 0 0 0 1 -1 0 0 0 0 1
x = 5×1
0.1 0 0 0 0
beta =
1e+10
U = 5×5
1.0e+00 * 1 -1 0 -1e+10 1e+10 0 1 -1 0 0 0 0 1 -1 0 0 0 0 1 -1 0 0 0 0 1
x = 5×1
0.1 0 0 0 0
beta =
1e+11
U = 5×5
1.0e+00 * 1 -1 0 -1e+11 1e+11 0 1 -1 0 0 0 0 1 -1 0 0 0 0 1 -1 0 0 0 0 1
x = 5×1
0.1 0 0 0 0
beta =
1e+12
U = 5×5
1.0e+00 * 1 -1 0 -1e+12 1e+12 0 1 -1 0 0 0 0 1 -1 0 0 0 0 1 -1 0 0 0 0 1
x = 5×1
0.1 0 0 0 0
function x = backsub(U,b)
%FORWARDSUB Solve a lower triangular linear system
%Input:
% U = Upper triangular matrix (n by n)
% b = right-hand side vector (n by 1)
% Output:
%Solution of Ux=b (n by 1 vector)
n = length(U);
x= zeros(n,1);
for i = 1:3
x(i) =( b(i) - U(i, 1:i-1)*x(1:i-1) ) / U(i,i);
end
end
Torsten
Torsten am 22 Sep. 2023
for i = 1:n
x(i) =( b(i) - U(i, 1:i-1)*x(1:i-1) ) / U(i,i);
end
Why do you use a code for forward substitution if you need backward substitution ?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Particle & Nuclear Physics finden Sie in Help Center und File Exchange

Produkte


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by