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 %...
using matlab Show the two formals or figures in time domin and z dominclc clear all...
using matlab Show the two formals or figures in time domin and z dominclc clear all b=[1,-4,5] a=[1,-6,11,-6] [R,P,C]=residuez(b,a) Ez=tf(b,a,1,'variable','z') zplane(b,a)
(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...
• In this script, write MATLAB code to create an array A of size 100 ×...
• In this script, write MATLAB code to create an array A of size 100 × 3. The first column should contain random numbers between 0 and 10. The second column should also contain random numbers between 0 and 10. The third column should contain random integers between 20 and 50. The first two columns represent the x and y coordinates of a map, respectively. The third column represents the height of buildings. For example, if the first row of...
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...
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...
Type the Python code for all the questions. 1 2 Q1: Using DataFrame, with Figure 1...
Type the Python code for all the questions. 1 2 Q1: Using DataFrame, with Figure 1 data, store the hypermarket data for rows and columns. Display the output as shown in Figure 1. 0 ITEMS NAME DISCOUNT 1 Grocery Rice 56 2 Electronics Mobile 25% 3 Food Apple 30% Figure 1 Q2: Update and display the "Rice" discount from 5% to 10%. Your output should be like Figure 2 now. 1 2 0 ITEMS NAME DISCOUNT 1 Grocery Rice 108...
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
Plot all four curves over 0 < t < 4 together using MATLAB. Post code in...
Plot all four curves over 0 < t < 4 together using MATLAB. Post code in the response. C1 : x=−5+2e−tcos20t y=−5+2e−tsin20t z=4t C2 : x=−5+2e−tcos20t y=5+2e−tsin20t z=4t C3 : x=5+2e−tcos20t y=−5+2e−tsin20t z=4t C4 : x=5+2e−tcos20t y=5+2e−tsin20t z=4t
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT