Performs the generalized app
2 views (last 30 days)
Show older comments
Please help to generalized the program for given n and input data read from a Excel file Aleator.xlsx with n=1024 or the general case n read as input or set from the begining?
D = xlsread('Aleat.xlsx',1);
n = 11;
nCols = 11
a11 = 0; a21 = 0 ; a31 = 0; a41 = 0; a51 = 0; a61 =0; a71=0; a81=0; a91=0; a101=0;
M = zeros(n,n);
M(1,1:n) = 0;
DM = zeros(10,10);
for i = 1:n
DM(i,1) = D(i,1)-D(i+1,1);
end;
for i = 1:(n-1)
DM(i,2) = DM(i,1)-DM(i+1,1);
end;
for i = 1:(n-2)
DM(i,3) = DM(i,2)-DM(i+1,2);
end;
for i = 1:(n-3)
DM(i,4) = DM(i,3)-DM(i+1,3);
end;
for i = 1:(n-4)
DM(i,5) = DM(i,4)-DM(i+1,4);
end;
for i = 1:(n-5)
DM(i,6) = DM(i,5)-DM(i+1,5);
end;
for i = 1:(n-6)
DM(i,7) = DM(i,6)-DM(i+1,6);
end;
for i = 1:(n-7)
DM(i,8) = DM(i,7)-DM(i+1,7);
end;
for i = 1:(n-8)
DM(i,9) = DM(i,8)-DM(i+1,8);
end;
for i = 1:(n-9)
DM(i,10) = DM(i,9)-DM(i+1,9);
end;
for i = 1:(n-10)
DM(i,11) = DM(i,10)-DM(i+1,10);
end;
b1 = D(14,1); .....b11 = D(4,1);
B1 = [b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11];
B2 = B1'
if size(DM, 1) < nCols
error('Insufficient number of equations.');
end
a = DM' \ B1';
X = linsolve(DM, B1');
a11 = a(1,1); a21 = a(2,1); a31 = a(3,1); a41 = a(4,1); a51 = a(5,1); a61 = a(6,1); a71 = a(7,1); a81 = a(8,1); a91 = a(9,1); a101 = a(10,1);
b1 = DM(1,1)*a11+DM(1,2)*a21+DM(1,3)*a31+DM(1,4)*a41+DM(1,5)*a51+DM(1,6)*a61+DM(1,7)*a71+DM(1,8)*a81+DM(1,9)*a91+DM(1,10)*a101;
b2 = ....
b10 = DM(10,1)*a11+DM(10,2)*a21+DM(10,3)*a31+DM(10,4)*a41+DM(10,5)*a51+DM(10,6)*a61+DM(10,7)*a71+DM(10,8)*a81+DM(10,9)*a91+DM(10,10)*a101;
a = DM' \ B1';
X = linsolve(DM, B1');
disp(a);
disp(X);
To read from a Excel file Aleator.xlsx with n=1024 or the general case n read as input or set from the begining?
Accepted Answer
Shubham
on 2 Oct 2024
Edited: Shubham
on 2 Oct 2024
Hey Viorel,
I can help you by providing some hints to generalize your MATLAB script.
- Use a for loop for initializing "DM(i,1)". For calculating the rest of the "DM" matrix, use nested loops:
for j = 1:n
for i = 1:(n-j)
DM(i, j+1) = DM(i, j) - DM(i+1, j);
end
end
- For constructing vectors such as "B1", simply extract the values from the matrix, for example:
B1 = D(1:n, 1);
- Instead of initializing various variables such as "a11", "a21" etc., construct a vector similar to B1 as shown above.
- For calculating the sum of products of various elements such as "b1 = DM(1,1)*a11+DM(1,2)* ....", simply use element wise multiplication between two vectors. For example:
Z = X.*Y
% X and Y are vectors of equal length
Lastly, use the computed vectors in the function linsolve to get the desired results.
I hope the above suggestions were helpful in generalizing your script.
3 Comments
Shubham
on 3 Oct 2024
Edited: Shubham
on 7 Oct 2024
In your scipt, you have initialized the B1 vector as following:
b1 = D(14,1); .....b11 = D(4,1);
B1 = [b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11];
You can refactor this as:
B1 = [D(14,1); D(13,1) .. D(4,1)];
And you can further refactor this as:
B1 = D(4:14,1);
B1 = flip(B1);
For example:
D = rand(14,14)
B1 = D(4:14,1);
B1 = flip(B1)
Similarly construct a vector A such as:
% A=[a11,a21 ... a101]
% A = [a(1,1), a(2,1) ...]
A = a(1:10,1)
Lastly, you are recalculating the variables b1,b2 ...b10. I do not see these variables being used again. A better way would be to use element wise multiplication or perform Matrix multiplication:
% a11 = a(1,1); a21 = a(2,1); a31 = a(3,1); a41 = a(4,1); a51 = a(5,1); a61 = a(6,1); a71 = a(7,1); a81 = a(8,1); a91 = a(9,1); a101 = a(10,1);
% b1 = DM(1,1)*a11+DM(1,2)*a21+DM(1,3)*a31+DM(1,4)*a41+DM(1,5)*a51+DM(1,6)*a61+DM(1,7)*a71+DM(1,8)*a81+DM(1,9)*a91+DM(1,10)*a101;
% b2 = ....
% b10 = DM(10,1)*a11+DM(10,2)*a21+DM(10,3)*a31+DM(10,4)*a41+DM(10,5)*a51+DM(10,6)*a61+DM(10,7)*a71+DM(10,8)*a81+DM(10,9)*a91+DM(10,10)*a101;
% Instead of the above method, you can do
ans = DM(1:10,1:10)*a(1:10,1);
I hope the above hints would be helpful!
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!