i am tring to input different z values to get different B using matrix manipulation. just having trouble using for loop
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
for z= 0.01:0.1:6
A = [-2.7/sqrt(20.25+z^2),3.2/sqrt(20.25+z^2),z/sqrt(20.25+z^2);3.2/sqrt(10.24+z^2),0,z/sqrt(10.24+z^2);-2.7/sqrt(23.29+z^2),4/sqrt(23.29+z^2),z/sqrt(23.29+z^2)];
c = [0;0;10000];
B = inv(A)*c
end
0 commentaires
Réponse acceptée
Stephen23
le 1 Fév 2023
Modifié(e) : Stephen23
le 1 Fév 2023
Note that I replaced the INV()* with the recommended MLDIVIDE:
Anyone who has read your code and the INV() documentation will make this recommendation.
c = [0;0;10000];
V = 0.01:0.1:6;
N = numel(V);
B = nan(numel(c),N);
for k = 1:N
z = V(k);
A = [-2.7/sqrt(20.25+z^2),3.2/sqrt(20.25+z^2),z/sqrt(20.25+z^2);3.2/sqrt(10.24+z^2),0,z/sqrt(10.24+z^2);-2.7/sqrt(23.29+z^2),4/sqrt(23.29+z^2),z/sqrt(23.29+z^2)];
B(:,k) = A\c; % recommended algorithm
end
B
0 commentaires
Plus de réponses (2)
Kunal Kandhari
le 1 Fév 2023
Code seems to be working!
Can you please elaborate what error you're getting?
Tushar Behera
le 1 Fév 2023
Modifié(e) : Tushar Behera
le 1 Fév 2023
Hi kaixi,
I am assuming you want to get different B values for different values of Z, and want to keep all the B values. However the code you have written will give B value for the last value of Z.
in order to solve this issue you can create B as an array and save all the instances of the solution inside B.
For example
z= 0.01:0.1:6
num=numel(z)
B=cell(num,1)
i=1;
for z= 0.01:0.1:6
A = [-2.7/sqrt(20.25+z^2),3.2/sqrt(20.25+z^2),z/sqrt(20.25+z^2);3.2/sqrt(10.24+z^2),0,z/sqrt(10.24+z^2);-2.7/sqrt(23.29+z^2),4/sqrt(23.29+z^2),z/sqrt(23.29+z^2)];
c = [0;0;10000];
answer= inv(A)*c
B{i}=answer;
i=i+1;
end
You can change the code as per your requirements. I hope this resolves your query.
Regards,
Tushar
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!