MATLAB Answers

Diagonal matrix, wishing to solve Ax=b

23 views (last 30 days)
Chandler Shimp
Chandler Shimp on 26 Jan 2020
Commented: Abel Medina on 6 Mar 2021
I am not getting any errors, but I am certainly doing something wrong.
I wish to get output that is the result of x for x=b/A
where A is an nxn matrix
b a vector
I am trying to create function called solveDiag(A,b)
instead of getting desired output i get the original matrix A
so my output is:
"ans =
2
the answer is
ans= [3 0 0
0 2 0
0 0 7]"
This what I have:
function [A,b]=solveDiag(A,b)
%A=nxn diagonal matrix
%b=column vector
%n is number of unknowns in matrix
A=[3 0 0 ; 0 2 0 ; 0 0 7];
b=[4 8 21]
for i = 1, 2,...,n do
x(1,:)=b(i)/A(i,i);
fprintf('the answer is',x(1,:))
end
end
  11 Comments
Chandler Shimp
Chandler Shimp on 27 Jan 2020
Thank you all very much, we have managed to create a function that delivers desired output!
Ignore the first line in command, i first used () to declare c instead of [].
Screen Shot 2020-01-27 at 11.04.03 AM.png

Sign in to comment.

Answers (1)

Les Beckham
Les Beckham on 26 Jan 2020
I thought that I had commented on this question earlier with something that should work for you. Here is the code that I think should work for you.
A = diag([3 2 7])
b = [4 8 21];
b/A
  3 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!