Ask your own question, for FREE!
Computer Science 8 Online
OpenStudy (anonymous):

Linear Algebra Programming. Write a C++ program that solves a linear system of n equations and n unknowns. Input: can be generated at random, a matrix of, float m[n][n+1] while n is entered from the key board. Output: a list of n values representing the n unknowns solution

OpenStudy (anonymous):

Well there is a few ways to do this message me if you need them

OpenStudy (anonymous):

yes please help @ahunter2222

OpenStudy (anonymous):

@Ahunter2222

OpenStudy (e.mccormick):

Have you gone over the mechanical ways to solve matrices? If so, it is a question of turning one of them into a program. Another question is if you need to deal with exact values or are decimal approximations allowed.

OpenStudy (anonymous):

the program need to ask the user for the output so it can be calculated. im having problem figuring how i need to start and the calculation..

OpenStudy (anonymous):

@e.mccormick

OpenStudy (e.mccormick):

Ask for the output? In a matrix as described you basically have: \(\begin{array}{ccc|c} a&b&c&x\\ d&e&f&y\\ g&h&i&z \end{array} \) If you divide the first row by a then replace it, that gets you a pivtot for a. Subtract d times row 1 from row 2. This eliminates d and preps that row. Do something similar to eliminate g. You keep doing this sort of mechancal process to eliminate variables. Eventually only values will exist in the spots for a, e, i, x, y, and z. The a, e, and i spots will be 1. x, y, and z will be solutions.

OpenStudy (e.mccormick):

I need to go, but that should give you something to think about.

OpenStudy (anonymous):

alright thanks

OpenStudy (e.mccormick):

Basically you want to use math to eliminate everything other than the diagonals and the far right answers. This is the reduced row echelon form of the matrix. If you have trouble with the concept as described, look that up: reduced row echelon form of a matrix You need to develop a computer way of getting that.

OpenStudy (anonymous):

so how would i eliminate b,c,f and h. will that not effect the values for a, d, g

OpenStudy (e.mccormick):

What is best is probably an example. This is a random one, so it is not going to be a nice answer, but it will show the process: \(\left[\begin{array}{ccc|c} 3 & 2 & 1 & 1 \\ 2 & 2 & 2 & 2 \\ 7 & 6 & 5 & 3 \end{array}\right]\) So for mechanical I can divide the first row by 3 to start. \(\left[\begin{array}{ccc|c} 1 & 2/3 & 1/3 & 1/3 \\ 2 & 2 & 2 & 2 \\ 7 & 6 & 5 & 3 \end{array}\right]\) Then I use this to knock out the first position in the next two rows. Because the second row is 2, I take -2 and multiply it through the first row: \(\begin{array}{ccc|c} 2 & -4/3 & -2/3 & -2/3 \\ \end{array}\) Then I add that and the second row: \(\begin{array}{ccc|c} 2 & -4/3 & -2/3 & -2/3 \\ 2 & 2 & 2 & 2 \\ \hline 0 & 2/3 & 13/3 & 7/3 \end{array}\) That means my matrix is now: \(\left[\begin{array}{ccc|c} 1 & 2/3 & 1/3 & 1/3 \\ 0 & 2/3 & 13/3 & 7/3 \\ 7 & 6 & 5 & 3 \end{array}\right]\) A similar process gets me a new row 3: \(\left[\begin{array}{ccc|c} 1 & 2/3 & 1/3 & 1/3 \\ 0 & 2/3 & 13/3 & 7/3 \\ 0 & 4/3 & 8/3 & 2/3 \end{array}\right]\) Now I want to turn row 2, column 2 into a pivot. It is 2/3, so I multiply the row by 3/2. \(\left[\begin{array}{ccc|c} 1 & 2/3 & 1/3 & 1/3 \\ 0 & 1 & 13/2 & 7/2 \\ 0 & 4/3 & 8/3 & 2/3 \end{array}\right]\) I could have left it in the form it was for the next step, but this is the mechanical process. You just need to break the process down into one a computer can do. You may also need to put in some checks to prevent dividing by 0. Now I use multiples of row 2 to eliminate spot 2 in rows 1 and 3. \(\begin{array}{ccc|c} 1 & 2/3 & 1/3 & 1/3 \\ 0 & -2/3 & -13/3 & -7/3 \\ \hline 1 & 0 & -12/3 & -6/3 \end{array}\) and \(\begin{array}{ccc|c} 0 & -4/3 & -26/3 & -14/3 \\ 0 & 4/3 & 8/3 & 2/3 \\ \hline 0 & 0 & -18/3 & -12/3 \end{array}\) So the original becomes: \(\left[\begin{array}{ccc|c} 1 & 0 & -12/3 & -6/3 \\ 0 & 1 & 13/2 & 7/2 \\ 0 & 0 & -18/3 & -12/3 \end{array}\right]\) I repeat all of this again for row 3. \(\left[\begin{array}{ccc|c} 1 & 0 & -12/3 & -6/3 \\ 0 & 1 & 13/2 & 7/2 \\ 0 & 0 & 1 & 2/3 \end{array}\right]\) \(\left[\begin{array}{ccc|c} 1 & 0 & 0 & 2/3 \\ 0 & 1 & 0 & -5/6 \\ 0 & 0 & 1 & 2/3 \end{array}\right]\) And unless I made a math mistake in all this, that would make the answers into: \(x_1=2/3\), \(x_2=-5/6\), and \(x_3=2/3\) Now, your program would be working in floats, so there will be a few other issues to deal with. Still, you should be able to get reasonably accurate results using a computer method that emulates this.

OpenStudy (anonymous):

Thank you

OpenStudy (e.mccormick):

There is one of the MIT Linear Algebra class videos where the professor talks about it too. Wish I could remember which one.

OpenStudy (anonymous):

That's okay, your example will help out alot.. i just need to to figure out how i should put it into codes

OpenStudy (anonymous):

i don't how the teacher wants the output to look like.. he wants us to do it his way

OpenStudy (anonymous):

r1=a/a;r2=b/a;r3=c/a;r4=x/a; r5=r1*d;r6=r2*d;r7=r3*d; r8=r4*d; r9=r5-d;r10=r6-e;r11=r7-f;r12=r8-y; r13=r1*g; r14=r2*g; r15=r3*g; r16=r4*g; r17=r13-g; r18=r14-h;r19=r15-i; r20=r16-z; r21=r9/r10; r22= r10/r10; r23= r11/10; r24=r12/r10; r25=-1*r9+r1;r26=-1*r10+r2;r27=-1*r11+r3;r28=-1*r12+r4; r29=-2*r9+r17;r30=-2*r10+r18;r31=-2*r11+r19;r32=-2*r12+r20; r33=r29/r31; r34=r30/r31;r35=r31/r31;r36=r32/r31; r37=r33*r23; r38=r34*r23; r39=r35*r23; r40=r36*r23; r41=r21-r37;r42=r22-r38;r43=r23-r39; r44=r24-r40; r45=r33*r27; r46=r34*r27; r47=r35*r27; r48=r36*r27; r49=r25+r44; r50=r26+r45; r51=r27+r46; r52=r28+r47;

OpenStudy (anonymous):

do u think this algorithm will work? @e.mccormick

OpenStudy (anonymous):

r49, r50, r51, r52 is the reduced first row r41, r42, r43, r44 is the reduced second row r33, r34, r35, r36 is the reduced third row

OpenStudy (e.mccormick):

Well, they will probably want it in an array format. That is what the whole m[n][n+1] thing is about. My example was a 3x3, but you will need the program to be able to do larger ones. Basically it will need to keep looping through the matrix until it gets to a solution or discovers no solution. I do not know if you are expected to account for systems that have no valid answers.

OpenStudy (anonymous):

i think he just wants us to do a 3x3 matrix, he is not clear on his explanations

OpenStudy (e.mccormick):

It says n is entered from the keyboard, so it is a variable.

OpenStudy (anonymous):

yeah, i'll figure something out. Thanks alot for your help

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!