Question

In: Advanced Math

This is Using MATLAB: I am trying to store the solution of this matrix because I...

This is Using MATLAB:

I am trying to store the solution of this matrix because I want to do something with the result like find the norm of that answer however I am stuck and cannot seem to be able to. Help would be appreciated!

---------------------------------------------

MATLAB CODE:

close all

clear

clc

A = [1 -1 2 -1;

2 -2 2 -3;

1 1 1 0;

1 -1 4 5];

b = [-8 -20 -2 4]';

x = gauss_elim(A,b)

function x = gauss_elim(A, b)

[nrow, ~] = size(A);

nb = length(b);

x = zeros(1,nrow);

  

% Gaussian elimination

for i = 1:nrow-1

if A(i,i) == 0

t = min(find(A(i+1:nrow,i) ~= 0) + i);

if isempty(t)

disp ('Error: A matrix is singular');

return

end

temp = A(i,:); tb = b(i);

A(i,:) = A(t,:); b(i) = b(t);

A(t,:) = temp; b(t) = tb;

end

for j = i+1:nrow

m = -A(j,i) / A(i,i);

A(j,i) = 0;

A(j,i+1:nrow) = A(j,i+1:nrow) + m*A(i,i+1:nrow);

b(j) = b(j) + m*b(i);

end

end

  

% Back substitution

x(nrow) = b(nrow) / A(nrow,nrow);

fprintf('\n\nHas exact solution:\n')

for i = nrow-1:-1:1

x(i) = (b(i) - sum(x(i+1:nrow) .* A(i,i+1:nrow))) / A(i,i);

end

end

Solutions

Expert Solution

I think you want to say that for different system you will get different solutions but you don't want to lose the previous solution you want to store it.
you can simply write a=x at the end

your x vector will be saved as a

if you want to save series of vectors in a matrix for different n number of systems.

I am modifying your code it will ask to enter matrix A and vector b, n number of times and then it will save your n solutions column wise in a matrix.

clc;

clear all;
n=input('enter number of times you want to enter A and b ');

for k=1:n
  
A=input('enter your matrix ');
b=input('enter b vector ');


x = gauss_elim(A,b);
mat(:,k)=x
end

function x = gauss_elim(A, b)

[nrow, ~] = size(A);

nb = length(b);

x = zeros(1,nrow);

  

% Gaussian elimination

for i = 1:nrow-1

if A(i,i) == 0

t = min(find(A(i+1:nrow,i) ~= 0) + i);

if isempty(t)

disp ('Error: A matrix is singular');

return

end

temp = A(i,:); tb = b(i);

A(i,:) = A(t,:); b(i) = b(t);

A(t,:) = temp; b(t) = tb;

end

for j = i+1:nrow

m = -A(j,i) / A(i,i);

A(j,i) = 0;

A(j,i+1:nrow) = A(j,i+1:nrow) + m*A(i,i+1:nrow);

b(j) = b(j) + m*b(i);

end

end

  

% Back substitution

x(nrow) = b(nrow) / A(nrow,nrow);

fprintf('\n\nHas exact solution:\n')

for i = nrow-1:-1:1

x(i) = (b(i) - sum(x(i+1:nrow) .* A(i,i+1:nrow))) / A(i,i);

end

end



Related Solutions

What I am trying to do is to design a Butterworth Bandpass filter using Matlab, High...
What I am trying to do is to design a Butterworth Bandpass filter using Matlab, High frequency must equal to 16 Hz and lower frequency must be 10Hz (passBand). and the input signal must be a white noise signal. here is my code : mu=0; sigma=2; X= sigma*randn(500,1)+mu; %Generating White Noise signal Fs=500;%Sampling Frequency Fh= 16; Fl=10; order=6; [b,a]=butter(order,[Fh Fl]/(Fs/2),'bandpass');%Butterworth BandPass filter XX=filtfilt(b,a,X);%filter the signal both forward and backword in time Actually, I am not sure about Fs value that...
I am trying to do edge detection using matlab. I have posted code here. It does...
I am trying to do edge detection using matlab. I have posted code here. It does not give any error but it's not running it at all. So please help me to fix it and also exaplin each line of this code pls. Thanks! i = imread('C:\Users\Amanda\Desktop"); I = rgb2gray(1i); BW1 = edge(I,'prewitt'); BW2= edge(I,'sobel'); BW3= edge(I,'roberts'); subplot (2,2,1); imshow(I); title('original'); subplot(2,2,2); imshow(BW1); title('Prewitt'); subplot(2,2,3); imshow(BW2); title('Sobel'); subplot(2,2,4); imshow(BW3); title('Roberts');
I am trying to plot a difficult function in MATLAB, however I need to understand the...
I am trying to plot a difficult function in MATLAB, however I need to understand the basics first. I am trying to plot n=0 for x=0:0.01:2pi n=n+1 y(n)=sin(x) end I beleive what this says, is that I want to plot sin(x) over a full period hence from o to 2pi, and I beleive the 0.01 is the incremenation along the x-axis. I am not sure what my n is doing Could smeone please graph this for me with the MATLAB...
Let's say that I am the marketer for a medical solution Company. We are trying to...
Let's say that I am the marketer for a medical solution Company. We are trying to enter the market with a medical product (Device) that Alleviates Feet pain and heals Heel Cracks To make you feel relaxed. This product is mainly Targeting People Age 22 To 40 Those who usually suffer from Feet pain and feet cracks. Currently, we are targeting the US market only. So I wanted to know how can I differentiate my product from my competitors and...
I am trying to implement a search function for a binary search tree. I am trying...
I am trying to implement a search function for a binary search tree. I am trying to get the output to print each element preceding the the target of the search. For example, in the code when I search for 19, the output should be "5-8-9-18-20-19" Please only modify the search function and also please walk me through what I did wrong. I am trying to figure this out. Here is my code: #include<iostream> using namespace std; class node {...
I am using a Macbook and trying to use the terminal and a VIM editor to...
I am using a Macbook and trying to use the terminal and a VIM editor to create program. This program will allow you to enter the costs of each ingredient and product, then prompt for the amount of each ingredient bought (individually by ingredient) and product sold (also individually), and from there show profit or loss, as well as profit on average for each unit sold. There will be say, 2 ingredients to buy and 3 products to sell. Please...
I am quantitating lead in a solution using anodic stripping Voltammetry. I am asked to use...
I am quantitating lead in a solution using anodic stripping Voltammetry. I am asked to use the Reagents as follows: 0.1 M KNO3 /50 mM HNO3 containing an unkown concentration of lead. For the purposes of preparing the method of standard additions, I am to assume the concentration in the container to be 1ppm. 0.1 M KNO3/50 mM HNO3 (take only what needed) 500 ppm Hg ^2+ in 0.1 M KNO3/50 mM HNO3 1000 ppm Pb and the preparation of...
Hello, i am currently working on an attendance system using MATLAB by the comparison of 2...
Hello, i am currently working on an attendance system using MATLAB by the comparison of 2 images of the same class, does anyone have any ideas or methods to use
I am trying to work on LCD display using VHDL. I want to display the word...
I am trying to work on LCD display using VHDL. I want to display the word “TECH” and make it blink on LCD. I need the complete source code (If anyone helps me and the code works I will offer bonus cash for it )
I am trying to tokenize a string using a function by passing the char string[] and...
I am trying to tokenize a string using a function by passing the char string[] and char *pointer[100]. While I have working code inside the int main(), I am having trouble actually declaring the parameters for the function. I know how to pass the char array (char string[]), but not how to pass the char pointer array (char *pointer[100]). This is my code below: int main() {    // Declare variables    char str[] = "this is a test only...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT