In this experiment note that the questions call for the creation of a function M
ID: 2079595 • Letter: I
Question
In this experiment note that the questions call for the creation of a function M-file to solve the problem. As before your function M-files will have to include help files, version information, error detection and extensive comments. You will also have to avoid bad practice such as choosing meaningless names for variables and/or functions and embedding universal numbers into the code. The module notes discuss the algorithms, sic Newton-Raphson algorithm, Gaussian elimination (with total pivoting) and Gauss-Seidel which you are called upon to employ, but they do not fully indicate how to implement these algorithms as MATLAB function M-files.
Problem 2 Create a MATLAB function M-file to solve an arbitrary system of linear equations by Gaussian elimination with total pivoting. Apply the resulting M-file to solve the system of linear equations (2). Is it possible to solve this system using Gaussian elimination without pivoting? Check your results by re-solving the equations using built-in MATLAB commands. The section on matrices in experiment MS01 offered two ways for you to solve a system of linear equations using built-in MATLAB commands.Explanation / Answer
MATLAB CODE:
clc;
clear all;
close all;
%Let below matrix represents system of linear equations
a =[0 3.2 0 1 -0.5 0; -2.1 2.2 7.5 -1.9 1 -2.5; 9.6 3 0 1.2 5.5 -7.5; 2 1 -5.7 -11 -1 6.1; 3 0 -3 -4 10 2.6];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Gauss elimination method [m,n)=size(a);
[m,n]=size(a);
for j=1:m-1
for z=2:m
if a(j,j)==0
t=a(j,:);a(j,:)=a(z,:);
a(z,:)=t;
end
end
for i=j+1:m
a(i,:)=a(i,:)-a(j,:)*(a(i,j)/a(j,j));
end
end
x=zeros(1,m);
for s=m:-1:1
c=0;
for k=2:m
c=c+a(s,k)*x(k);
end
x(s)=(a(s,n)-c)/a(s,s);
end
x'
OUTPUT:
ans =
-0.8769
0.1328
-0.7266
-0.3407
0.1688
>>
Verification:
A=[0 3.2 0 1 -0.5; -2.1 2.2 7.5 -1.9 1; 9.6 3 0 1.2 5.5; 2 1 -5.7 -11 -1 ; 3 0 -3 -4 10];
B=[0;-2.5;-7.5;6.1;2.6];
x=inv(A)*B
x =
-0.8769
0.1328
-0.7266
-0.3407
0.1688
>>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.