필터 지우기
필터 지우기

i am tring to input different z values to get different B using matrix manipulation. just having trouble using for loop

조회 수: 1 (최근 30일)
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

채택된 답변

Stephen23
Stephen23 2023년 2월 1일
편집: Stephen23 2023년 2월 1일
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

추가 답변 (2개)

Kunal Kandhari
Kunal Kandhari 2023년 2월 1일
Code seems to be working!
Can you please elaborate what error you're getting?
  댓글 수: 1
kaixi gu
kaixi gu 2023년 2월 1일
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.

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


Tushar Behera
Tushar Behera 2023년 2월 1일
편집: Tushar Behera 2023년 2월 1일
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

카테고리

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

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by