Help with creation/alteration of a tri-diagonal matrix, using loops/ alternatives...
3 views (last 30 days)
Show older comments
Okay so I need to create a tri-diagonal matrix (K) which can be used to calculate pressure distribution using the equation p=K/d (d being another matrix). The following is what I have so far, A,B, and C are each a range of 18 numbers (I need 20 for each), and I need these ranges of values within the tri-diagonal matrix. (There is also a previous function containing inputs and calculations which provide values for this function.
x=linspace(-pi/2,pi/2,20);
fprintf('x: %g \n', x)
delta=pi/19;
for n=2:19
A=(h(n).^3/delta.^2) - (h(n-1).^3-h(n+1).^3)/(4*delta.^2);
fprintf('A: %g \n', A)
end
for n=2:19
B=2*(h(n).^3)/delta.^2;
fprintf('B: %g \n', B)
end
for n=2:19
C=(h(n).^3/delta.^2) + ((h(n-1).^3-h(n+1).^3)/4*delta.^2);
fprintf('C: %g \n', C)
end
for i=2:20;
K(i,i-1)=A;
K(i,i)=B;
K(i,i+1)=C;
end
disp(K)
However, when I run this it displays as:
x: -1.5708
x: -1.40545
x: -1.2401
x: -1.07476
x: -0.909408
x: -0.744061
x: -0.578714
x: -0.413367
x: -0.24802
x: -0.0826735
x: 0.0826735
x: 0.24802
x: 0.413367
x: 0.578714
x: 0.744061
x: 0.909408
x: 1.07476
x: 1.2401
x: 1.40545
x: 1.5708
A: -0.0332221
A: -0.036702
A: -0.245881
A: -0.765036
A: -1.57187
A: -2.53299
A: -3.44764
A: -4.10745
A: -4.35622
A: -4.13381
A: -3.49295
A: -2.58437
A: -1.61433
A: -0.785464
A: -0.236645
A: 0.00210519
A: 0.0269668
A: 0.0441213
B: 0.0108993
B: -0.00973517
B: -0.243776
B: -1.00168
B: -2.35734
B: -4.14732
B: -6.03201
B: -7.6004
B: -8.49003
B: -8.49003
B: -7.6004
B: -6.03201
B: -4.14732
B: -2.35734
B: -1.00168
B: -0.243776
B: -0.00973517
B: 0.0108993
C: 0.00547854
C: -0.00484379
C: -0.121795
C: -0.500643
C: -1.17838
C: -2.07332
C: -3.01568
C: -3.79997
C: -4.24493
C: -4.2451
C: -3.80043
C: -3.01633
C: -2.074
C: -1.17896
C: -0.501038
C: -0.121981
C: -0.00489138
C: 0.00542073
1.0e-002 *
Columns 1 to 13
0 0 0 0 0 0 0 0 0 0 0 0 0
4.4121 1.0899 0.5421 0 0 0 0 0 0 0 0 0 0
0 4.4121 1.0899 0.5421 0 0 0 0 0 0 0 0 0
0 0 4.4121 1.0899 0.5421 0 0 0 0 0 0 0 0
0 0 0 4.4121 1.0899 0.5421 0 0 0 0 0 0 0
0 0 0 0 4.4121 1.0899 0.5421 0 0 0 0 0 0
0 0 0 0 0 4.4121 1.0899 0.5421 0 0 0 0 0
0 0 0 0 0 0 4.4121 1.0899 0.5421 0 0 0 0
0 0 0 0 0 0 0 4.4121 1.0899 0.5421 0 0 0
0 0 0 0 0 0 0 0 4.4121 1.0899 0.5421 0 0
0 0 0 0 0 0 0 0 0 4.4121 1.0899 0.5421 0
0 0 0 0 0 0 0 0 0 0 4.4121 1.0899 0.5421
0 0 0 0 0 0 0 0 0 0 0 4.4121 1.0899
0 0 0 0 0 0 0 0 0 0 0 0 4.4121
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
Columns 14 to 21
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0.5421 0 0 0 0 0 0 0
1.0899 0.5421 0 0 0 0 0 0
4.4121 1.0899 0.5421 0 0 0 0 0
0 4.4121 1.0899 0.5421 0 0 0 0
0 0 4.4121 1.0899 0.5421 0 0 0
0 0 0 4.4121 1.0899 0.5421 0 0
0 0 0 0 4.4121 1.0899 0.5421 0
0 0 0 0 0 4.4121 1.0899 0.5421
(Firstly, apologies for columns 1 - 13 looking atrocious, 14 - 21 gives a better idea) As can be seen, each of the values within each A, B, and C are identical whereas I need the range displayed by fprintf. Furthermore, I need to get rid of the first and last columns, and the first row to give me the correct matrix. If anyone has any information whether a solution or just a pointer in the right direction it will be greatly appreciated as I have been racking my brain over this for a while!
Thanks
0 Comments
Answers (1)
Andrei Bobrov
on 3 Jan 2014
Edited: Andrei Bobrov
on 3 Jan 2014
m = numel(h);
h = h(:);
d2 = delta.^2;
A = conv2(h,[1/4;1;-1/4],'valid')/d2;
B = 2*h(2:end-1)/d2;
C = conv2(h,[-1/4;1;1/4],'valid')/d2;
lo = triu(tril(ones(m+1,m-2)),-2);
lo(lo>0) = [A;B;C];
out = lo';
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!