i am tring to input different z values to get different B using matrix manipulation. just having trouble using for loop
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
kaixi gu
el 1 de Feb. de 2023
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 comentarios
Respuesta aceptada
Stephen23
el 1 de Feb. de 2023
Editada: Stephen23
el 1 de Feb. de 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 comentarios
Más respuestas (2)
Kunal Kandhari
el 1 de Feb. de 2023
Code seems to be working!
Can you please elaborate what error you're getting?
Tushar Behera
el 1 de Feb. de 2023
Editada: Tushar Behera
el 1 de Feb. de 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
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!