Question

In: Mechanical Engineering

Using this sample matlab code: clear all; clc A= ????????; B= ????????; AUG=[A B]; for L=1:size(A,2)...

Using this sample matlab code:

clear all;
clc
A= ????????;
B= ????????;
AUG=[A B];
for L=1:size(A,2)
%Pivoting starts
for k=L:size(AUG,1)
for m=k+1:size(AUG,1)
if abs(AUG(k,L))<abs(AUG(m,L))
temp=AUG(m,:);
AUG(m,:)=?????????;
AUG(k,:)=?????????;
end
end
end
%Pivoting ends
%Gauss Elimination starts
for k=L+1:size(AUG,1)
AUG(k,:)= ????????????????????????????;
AA=AUG(:,1:size(A,2))
BB=AUG(:,size(A,2)+1:end)
end
%Gauss Elimination ends
end

b)Write a MATLAB M-file which performs gauss elimination without pivoting step by step and shows the coefficient matrix in each step. Using cond (X, P) calculate the condition number of the final step coefficient matrix (U matrix).

c) Write a MATLAB M-file which performs gauss elimination with pivoting step by step and shows the coefficient matrix in each step.

d)Using cond (X, P) calculate the condition number of the final step coefficient matrix (U matrix).

Solutions

Expert Solution

clc; clear;
%% Gauss elimination WITHOUT Pivoting
A = [3 1 4; -1 5 2; 3 1 7];
b = [1; 4; 7];
[n, n] = size(A);
% Check for zero diagonal elements
if any(diag(A)==0)
error('Division by zero will occur; pivoting not supported')
end
% Forward elimination
for row=1:n-1
for i=row+1:n
factor = A(i,row) / A(row,row);
for j = row:n
A(i,j) = A(i,j) - factor*A(row,j);
end
b(i) = b(i) - factor*b(row);
end
A_and_b = [A b]
end
% Backward substitution
x(n) = b(n) / A(n,n);
for row = n-1:-1:1
sums = b(row);
for j = row+1: n
sums = sums - A(row,j) * x(j);
end
x(row) = sums / A(row,row);
end
p1 = 1 ;
C1 = cond(A,p1)
%% Gauss elimination WITH Pivoting
A = [3 1 4; -1 5 2; 3 1 7];
b = [1; 4; 7];
% Create permutation vector
n = size(A, 1); % Size of input matrix
r = zeros(n, 1); % Initialize permutation vector
for i = 1 : 1 : n   
r(i) = i;
end
% Apply Gaussian elimination and rearrange permutation vector
x = zeros(n, 1); % Initialize solution vector
for k = 1 : 1 : n % Go through each element in permutation vector   
% Compare each element in r(k)th column for the max
max = abs(A(r(k), r(k)));   
max_pos = k;   
for l = k : 1 : n   
if abs(A(r(l), r(k))) > max   
max = abs(A(r(l), r(k)));   
max_pos = l;   
end
end
% Switch the kth r-vector element with max r-vector element
temp_r = r;
r(k) = temp_r(max_pos);
r(max_pos) = temp_r(k);
% Eliminate A-vector elements in r(k)th column below r(k)th row   
for i = 1 : 1 : n
if i ~= k
zeta = A(r(i), k) / A(r(k), k);
for j = k : 1 : n
A(r(i), j) = A(r(i), j) - A(r(k), j) * zeta;
end
b(r(i)) = b(r(i)) - b(r(k)) * zeta;
end
end
end
% Compute the solution frpm the diagonalized A-matrix
for i = 1 : 1 : n
x(i) = b(r(i)) / A(r(i), i)
end
p2 = 1 ;
C2 = cond(A,p2)


Related Solutions

explain this Matlab code and finish its last line. %% clear all; clc; %% % Données...
explain this Matlab code and finish its last line. %% clear all; clc; %% % Données de départ NbrEchParPer=128*2; NbrPer=16; % Ensuite essayer avec 512 T=1e-3; % calcul préliminaire fs=NbrEchParPer/(T); NbrEch=NbrEchParPer*NbrPer; %Axes des temps et des élongations t=[0:NbrEch-1]./fs; x=repmat([ones(1,NbrEchParPer/2),zeros(1,NbrEchParPer/2)] ,1,NbrPer); %représentation des signaux temporels fig1=figure(1);clf; subplot(4,1,[1 2],'Parent',fig1,'Color',[0 0 0]); hold on; plot(t,x,'.b'); plot(t,x,'-g'); ylim([-1,2]); %% %Calcul de la FFT X=fft(x); %Axe des fréquences df=fs/((NbrEch-mod(NbrEch,2))/2); f=[0:NbrEch-1].*df; %représentation du spectre subplot(4,1,3,'Parent',fig1,'Color',[0 0 0]); hold on; plot(f,abs(X),'m'); subplot(4,1,4,'Parent',fig1,'Color',[0 0 0]); hold on; plot(f,abs(X)XXXXXXXXXXXX,'m');
Fix the bugs in this matlab program so that it solves. clear clf clc time =...
Fix the bugs in this matlab program so that it solves. clear clf clc time = linspace(0, 5, 100); m = 1; k = 100; c = 1; delta = 0.2; [period, response] = Exmp(m, k, c, delta, time); plot(time, response) grid %Exmp(m, k, c, delta, time) % %________________________________________ function [T, x] = Exmp(m, k, c, delta, t) omega = sqrt(k/m); cC = 2*m*omega; if c>= cC disp('Not an underdamped system') T = 0; x = 0; return; end %...
(1)Using the Matlab code developed in Software Assignment #1: a. Convert the code that generates the...
(1)Using the Matlab code developed in Software Assignment #1: a. Convert the code that generates the random number (H,T) with equal probabilities into a function called myBernolli(p, S) that takes as an input the probability of success p and S is the outcome defined as success (either T or H) and returns the outcome of the trial (either T or H). b. Test that your function is actually producing the successful outcome with probability p by running the function in...
USING MATLAB Part 2: Insert coins For this part, you are going implement the code that...
USING MATLAB Part 2: Insert coins For this part, you are going implement the code that asks the user to enter coins until they have entered enough for the NAU power juice. Open the insert_coins.m file. Initialize total to 0. We do this because initially, no coins have been entered. Using a loop, ask the user to enter a coin until the total matches or exceeds 115 cents. The input should be a char or string, so make sure that...
The following code must be written using matlab How to loop through a vector in matlab...
The following code must be written using matlab How to loop through a vector in matlab and assigning a value to every 4th entry. The vector could be of any length. Thanks
By using MATLAB 1. Divide a speech signal or music signal into frames of size N...
By using MATLAB 1. Divide a speech signal or music signal into frames of size N = 64. 2. Compute the N?point DFT of each frame. 3. Save only first L = 20 DFT coefficients. 4. Reconstruct the frame from these L coefficients. (Do not disturb the symmetry of DFT during inverse DFT computation. X[k] = X[N ?k] for real signals, N = 64 here. Pad zeros for missing DFT coefficients.) 5. Comment on the quality of reconstructed and the...
Using newer versions of MATLAB, please write the complete code for: 1. Assume Y is an...
Using newer versions of MATLAB, please write the complete code for: 1. Assume Y is an exponential random variable with rate parameter λ=2. (1) Generate 1000 samples from this exponential distribution using inverse transform method (2) Compare the histogram of your samples with the true density of Y.
Hello, I need the Matlab code of the Fourier Transform without using the Matlab functions fft...
Hello, I need the Matlab code of the Fourier Transform without using the Matlab functions fft and dft. Applied to discrete signals. If you can with an example.Thank you!!
Matlab code problems I have to write a code using functions to find out if a...
Matlab code problems I have to write a code using functions to find out if a year is a leap year. I'm on the write track i feel like but I keep getting an error message and matlab isnt helping to troubleshoot. The issue is on line 30. Here is my code: function project_7_mfp() %   PROJECT_7_ABC   project_7_abc() creates a function that prompts the user %   enter a year after 1582 It then determines if it is a leap year %...
I want the code for the 2D Ising model using Matlab
I want the code for the 2D Ising model using Matlab
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT