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
댓글 수: 0
채택된 답변
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
댓글 수: 0
추가 답변 (2개)
Kunal Kandhari
2023년 2월 1일
Code seems to be working!
Can you please elaborate what error you're getting?
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
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!