\"Write a user-defined MATLAB function that solves.....\" Could anyone show me h
ID: 2084868 • Letter: #
Question
"Write a user-defined MATLAB function that solves....." Could anyone show me how to do this? Like the matlab code please. And if id = 72, how shall I set variables to calculate the x roots? Thank you!
Write a user-defined MATLAB function that solves a system of n linear equations, [al [x] -lb], with the Gauss-Seidel iteration method. For the function name and arguments use x-Gasdl (a, b) where a is the matrix of coefficients, b is the right-hand-side column of constants, and x is the solution. Then, use the GaSdl function, with a zero starting guess, to solve the system 112 3z2 -z3 +824=15. where, in the 2nd equation, "id" is the last two digits of your student id. The decision to stop the iteration is based on the criterion of infinity norm of the residual r"- )iri The residual is defined as [ra)] = [b]-[ake], obtained from the numerical solution of rNS after k iterations. xl x4Explanation / Answer
The function declaration would be x=GaSdl(a,b)
where a & b are matrix as given in question.
function x=GaSdl(a ,b)
%function declaration is according to the question
%Here since a & b are already given in question
x1=0; x2=0; x3=0; x4=0;
%zero initial guess according to the question
% we now create variables to store data from iterations
k1=(b(1)-a(1,2)*x2-a(1,3)*x3-a(1,4)*x4)/a(1,1);
k2=(b(2)-a(2,1)*x1-a(2,3)*x3-a(2,4)*x4)/a(2,2);
k3=(b(3)-a(3,1)*x1-a(3,2)*x2-a(3,4)*x4)/a(3,3);
k4=(b(4)-a(4,1)*x1-a(4,2)*x2-a(4,3)*x3)/a(4,4);
%These are iterations for corresponding variables according to gauss siedel
%method
while abs(k1-x1)>10^(-3) && abs(k2-x2)>10^(-3) && abs(k3-x3)>10^(-3) && abs(k4-x4)>10^(-3)
%the condiition is set according to question to get accuracy upto 3rd
%decimal point
x1=k1; x2=k2; x3=k3; x4=k4;
%The values of variables are set to founnd data from previos iteration
k1=(b(1)-a(1,2)*x2-a(1,3)*x3-a(1,4)*x4)/a(1,1);
k2=(b(2)-a(2,1)*x1-a(2,3)*x3-a(2,4)*x4)/a(2,2);
k3=(b(3)-a(3,1)*x1-a(3,2)*x2-a(3,4)*x4)/a(3,3);
k4=(b(4)-a(4,1)*x1-a(4,2)*x2-a(4,3)*x3)/a(4,4);
%The iterations continue
end
x=[x1, x2,x3,x4 ];
The answers are :
x1=1.43782031806259
x2=6.81834794511603
x3=-0.784604919731544
x4=-0.778176589610909
If any doubt, please let me know in the comments.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.