Performs the generalized app

조회 수: 2 (최근 30일)
Viorel-Mihai Popescu
Viorel-Mihai Popescu 2024년 10월 2일
편집: Shubham 2024년 10월 7일
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;
Incorrect use of '=' operator. Assign a value to a variable using '=' and compare values for equality using '=='.
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?
  댓글 수: 1
Rik
Rik 2024년 10월 2일
Your question is not clear to me.
Have a read here and here. It will greatly improve your chances of getting an answer.
It looks like you should not attempt to use numbered variables, but use arrays instead. The syntax error doesn't help with determining what you want to know.

댓글을 달려면 로그인하십시오.

채택된 답변

Shubham
Shubham 2024년 10월 2일
편집: Shubham 2024년 10월 2일
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
Shubham
Shubham 2024년 10월 3일
편집: Shubham 2024년 10월 7일
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)
D = 14×14
0.4522 0.6178 0.6383 0.5703 0.5761 0.6198 0.2209 0.0520 0.5294 0.5361 0.5000 0.9394 0.2257 0.9865 0.6800 0.3840 0.7348 0.2485 0.4091 0.6236 0.4972 0.0185 0.8801 0.6002 0.7392 0.5549 0.5406 0.6253 0.7884 0.1331 0.3340 0.7779 0.0469 0.8905 0.4673 0.9398 0.3630 0.8698 0.3734 0.6390 0.5001 0.4775 0.4826 0.2813 0.4971 0.7623 0.7489 0.5930 0.0490 0.3416 0.8871 0.6565 0.7775 0.4893 0.2521 0.3973 0.6896 0.5991 0.1800 0.9964 0.3872 0.9030 0.2497 0.4350 0.2264 0.1773 0.0068 0.3231 0.8631 0.9510 0.4742 0.3974 0.5570 0.7723 0.5700 0.3324 0.3428 0.2456 0.7109 0.8937 0.6922 0.8365 0.9439 0.4498 0.3323 0.3899 0.6496 0.2128 0.2174 0.3922 0.1657 0.5232 0.0628 0.9700 0.7917 0.3881 0.0680 0.8173 0.4684 0.5144 0.1312 0.4164 0.0032 0.0084 0.7431 0.6999 0.6164 0.4232 0.8862 0.0304 0.0599 0.7623 0.6631 0.6060 0.1806 0.9485 0.8156 0.7263 0.7975 0.0098 0.8366 0.3342 0.7889 0.5918 0.1993 0.0095 0.0071 0.7020 0.2715 0.1391 0.7615 0.1120 0.3722 0.3590 0.2008 0.3713 0.3961 0.2821 0.3401 0.7067
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
B1 = D(4:14,1);
B1 = flip(B1)
B1 = 11×1
0.6389 0.2615 0.7857 0.5669 0.0071 0.6631 0.4684 0.3323 0.4742 0.6896
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Example for "flip" function can be found here:
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!
Viorel-Mihai Popescu
Viorel-Mihai Popescu 2024년 10월 3일
It is very helpful but the calculus is not working, it is null:
I think the equality DM = zeros(n,n);
DM(i:n,1)= D(i:n,1)
gives null rezult...
I little help please

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by