Example 4-6: MATLAB user-defined function for solving a system of equations usin
ID: 3167615 • Letter: E
Question
Example 4-6: MATLAB user-defined function for solving a system of equations usin LU decomposition with Crout's method. Determine the currents i, i2, i, and i4 in the circuit shown in the figure (same as in Fig. 4-1).Write the system of equations that has to be solved in the form [a][i] = [b]. Solve the system by using the LU decomposition method, and use Crout's method for doing the decomposition. SOLUTION 3 24v 40 2 The currents are determined from the set of four equations, Eq. (4.) 6 The equations are derived by using Kirchhoft's law. In matrix form, 40 alli [bl, the equations are: 9 4-2 024 -4 17-6-31 1,1 = 1-16 -2-6 14-60 0 -3-6 11 8 (4.44) To solve the system of equations, three user-defined functions are created. The functions are as fol- lows:Explanation / Answer
Matlab code
clc
clear all
a = [9 -4 -2 0
-4 17 -6 -3
-2 -6 14 -6
0 -3 -6 11];
[m,n]=size(a);
for j=1:m-1
for z=j+1: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
disp('Gauss elimination method:');
a
x'
[m,n]=size(a);
for j=1:m-1
for z=2:m
if a(j,j)==0
t=a(1,:);a(1,:)=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
for j=m:-1:2
for i=j-1:-1:1
a(i,:)=a(i,:)-a(j,:)*(a(i,j)/a(j,j));
end
end
for s=1:m
a(s,:)=a(s,:)/a(s,s);
x(s)=a(s,n);
end
disp('Gauss-Jordan method:');
a
x'
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.