Azzera filtri
Azzera filtri

I am supposed to write a function that takes three inputs: 1) a Coeff matrix A(nxn square mtx), right side vector b, and approximate soln' vector Xo. The function is then supposed to compute the residual vector r.

1 visualizzazione (ultimi 30 giorni)
This is the code I have so far but I believe this is wrong since the function doesn't ask me for any inputs it outputs what I have inputted then I can calculate r...Any help would be greatly appreciated. I am still trying to learn Matlab
basically I want to be able to input a 4x4 mtx, the right hand side vector, the soln vector , then the function calculates the residual vector. it does so without it being a function but I would like to make it a function Thank You
function[] = residv(A, b, x)
A = [1 3 -1; -1 3 4; 5 6 7]
b = [2;3;8]
x = A \ b;
%x = inv(A)*b
r = b - A*x;

Risposta accettata

James Tursa
James Tursa il 5 Ott 2017
Modificato: James Tursa il 6 Ott 2017
To my reading of the assignment, you are making this task more involved than it really is. The assignment states you are given A, b, and Xo. Then you are to calculate and return the residual r. So your function should look something like this:
function r = residv(A, b, Xo)
r = _______; % <-- you fill this in. an expression involving A, b, and Xo
return
end
  1 Commento
Janvier Solaris
Janvier Solaris il 6 Ott 2017
This is similar to the answer above Thank you, I tried both implementations and they worked I fed the workspace A, b, and Xo, then I called the function and it gave me the result Again Thank you

Accedi per commentare.

Più risposte (1)

Walter Roberson
Walter Roberson il 5 Ott 2017
You would do the
A = [1 3 -1; -1 3 4; 5 6 7]
b = [2;3;8]
x = A \ b;
outside of the code for the function, and you would invoke the function passing those in, like
A = [1 3 -1; -1 3 4; 5 6 7]
b = [2;3;8]
x = rand(3,1);
residv(A, b, x)
Your function is probably supposed to output r
  1 Commento
Janvier Solaris
Janvier Solaris il 6 Ott 2017
Thank you for your prompt answer; This works. If I understood correctly. Build the function then define it, then feed it the variables and it will compute the results. ?

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by