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)
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

Respuesta aceptada

Stephen23
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
B = 3×60
1.0e+07 * 0.0033 0.0033 0.0033 0.0033 0.0033 0.0033 0.0033 0.0033 0.0033 0.0033 0.0033 0.0034 0.0034 0.0034 0.0034 0.0034 0.0034 0.0035 0.0035 0.0035 0.0035 0.0036 0.0036 0.0036 0.0037 0.0037 0.0037 0.0038 0.0038 0.0038 0.0060 0.0060 0.0060 0.0060 0.0061 0.0061 0.0061 0.0061 0.0061 0.0061 0.0062 0.0062 0.0062 0.0063 0.0063 0.0063 0.0064 0.0064 0.0064 0.0065 0.0065 0.0066 0.0066 0.0067 0.0067 0.0068 0.0069 0.0069 0.0070 0.0070 -1.0470 -0.0952 -0.0499 -0.0338 -0.0256 -0.0206 -0.0173 -0.0149 -0.0131 -0.0117 -0.0106 -0.0097 -0.0089 -0.0083 -0.0077 -0.0073 -0.0069 -0.0065 -0.0062 -0.0059 -0.0056 -0.0054 -0.0052 -0.0050 -0.0049 -0.0047 -0.0046 -0.0044 -0.0043 -0.0042

Más respuestas (2)

Kunal Kandhari
Kunal Kandhari el 1 de Feb. de 2023
Code seems to be working!
Can you please elaborate what error you're getting?
  1 comentario
kaixi gu
kaixi gu el 1 de Feb. de 2023
the z should be a set of input value go into the matices. i am tring to get different 1*3 B as an output respect to the different input z. i just dont know how to use the for loop to get it.

Iniciar sesión para comentar.


Tushar Behera
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

Categorías

Más información sobre MATLAB en Help Center y File Exchange.

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by