0% found this document useful (0 votes)
138 views

MATH15 Project JavaScript Source Code

This document contains JavaScript code to solve systems of linear equations using Gauss-Jordan elimination. It defines functions to read in the coefficients, perform row operations to convert the matrix to row echelon form, and output the solution. The program prompts the user for the number of equations and variables, reads in the coefficients, and then iteratively makes the first element of each row 1 by dividing and subtracting rows, outputting the results after each step until it reaches the final solution.

Uploaded by

Ivan Perez
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
138 views

MATH15 Project JavaScript Source Code

This document contains JavaScript code to solve systems of linear equations using Gauss-Jordan elimination. It defines functions to read in the coefficients, perform row operations to convert the matrix to row echelon form, and output the solution. The program prompts the user for the number of equations and variables, reads in the coefficients, and then iteratively makes the first element of each row 1 by dividing and subtracting rows, outputting the results after each step until it reaches the final solution.

Uploaded by

Ivan Perez
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

<head>

<title>Gauss-Jordan Matrix Solutions Using JavaScript</title>


</head>
<body>
<pre>
<script language="javascript">
<!--//
document.write("This program solves a system of linear equations by way of ");
document.write("Gauss-Jordan.");
document.write("<br>");
document.write("You should always check the results of this program by");
document.write(" another means other than this program!");
document.write("<br>");
document.write("<br>");
//******************************************************************
//Function to define array elements;
function defineelements(){
lengthofequation=equation[i].length;
builder="";
count=0;
for(count==0;count<lengthofequation;count++){
builder=equation[i].charAt(count);
// document.write(builder+"<br>");
if(builder!=" "){
rowcolumn[i][j]=rowcolumn[i][j]+builder;
a[i][j]=parseFloat(rowcolumn[i][j]);
//document.write(i+" "+j+" "+rowcolumn[i][j]+"<br>");
//document.write(i+" "+j+" "+a[i][j]+"<br>");
};
if(builder==" "){
j=j+1;
};
builder="";
};
};//End of function defineelements;
//*********************************************************************
function makefirstelement1(){
j=1;
for(j==1;j<=numberofcolumns;j++){
a[rowtoone][j]=a[rowtoone][j]/divisor;
};
};//End of function firstelement1;
//*********************************************************************
function multiplyaddtorow(){
jj=1;
for(jj==1;jj<=numberofcolumns;jj++){
a[rowtozero][jj]=a[rowtozero][jj]-multiplier*a[rowtoone][jj];
};
};//End of function multiplyaddtorow;
//**********************************************************************
//*** Function printsolution
function printsolution(){
i=1;j=1
for(i=1;i<=numberofrows;i=i+1){
for(j=1;j<=numberofcolumns;j=j+1){
if(Math.abs(a[i][j])<=.000001){
a[i][j]=0;//Zero out scientific notation
}
}

}
i=1;j=1;
for(i=1;i<=numberofrows;i++){
for(j=1;j<=numberofcolumns;j++){
rowcolumn[i][j]=""+a[i][j]+"
";
rowcolumn[i][j]=rowcolumn[i][j].substring(0,15);
document.write(rowcolumn[i][j]+" ");
if(j==numberofcolumns){
document.write("<br>");
document.write("<br>");
};
};
};
if(rowtoone!=numberofrows){
document.write("NEXT STEP ************************************************");
document.write("*********************");
document.write("<br>");
document.write("<br>");
};
if(rowtoone==numberofrows){
document.write("FINISHED *************************************************");
document.write("*********************");
};
};//End of function printsolution;
//**********************************************************************
//*******
//Main Body
//*******
//**********************************************************************
numberofequations=prompt("How many equations are there for this system?","");
numberofrows=numberofequations;
numberofcolumns=prompt("How many columns are there?","");
equation=new Array(numberofcolumns+1);
k=0;
//Set all elements to 0.;
for(k==0;k<=numberofcolumns+1;k++){
equation[k]=0;
};
i=0;
j=0;
//Set all elements to 0 or to null.;
rowcolumn=new Array(numberofrows);//String;
a=new Array(numberofrows);//Real number;
for(i=0;i<=numberofrows;i++){
//********NOTE: Use i=0 not i==0 for nested loops;
rowcolumn[i]=new Array(numberofcolumns);
a[i]=new Array(numberofcolumns);
for(j=0;j<=numberofcolumns;j++){
rowcolumn[i][j]="";
a[i][j]=0;
};
};
pp="Enter the elements of one equation as decimals separated by a space,";
q="\n then press the enter key.";
qq="\nExample: 2x - 3y = -4 would be 2 -3 -4 then enter key.";
p=pp+q+qq;
i=1;
for(i==1;i<=numberofequations;i++){
equation[i]=prompt(p,"");

};
//************************************************************************
//Call functions
//***
i=1;
j=1;
for(i==1;i<=numberofequations;i++){
defineelements();
j=1;
};
document.write("Step by step solution follows:");
document.write("<br>");
document.write("<br>");
rowtoone=1;
printsolution();
//***Row reduced Echelon form follows:
rowtoone=1;
for(rowtoone=1;rowtoone<=numberofrows;rowtoone++){
columntoone=rowtoone;
divisor=a[rowtoone][columntoone];
makefirstelement1();
firstrowbelow=rowtoone+1;
rowbelow=firstrowbelow;
if(firstrowbelow<=numberofrows){
for(rowbelow=firstrowbelow;rowbelow<=numberofrows;rowbelow++){
columntozero=columntoone;
multiplier=a[rowbelow][columntozero];
rowtozero=rowbelow;//Below the row with a one.
multiplyaddtorow();
};
};
firstrowabove=rowtoone-1;
rowabove=firstrowabove;
if(firstrowabove>=1){
for(rowabove=firstrowabove;rowabove>=1;rowabove--){
columntozero=columntoone;
multiplier=a[rowabove][columntozero];
rowtozero=rowabove;//Above the row with a one.
multiplyaddtorow();
};//End of 'for'
};//End of 'if'
printsolution();
};//End of row reduced Echelon form
//-->
</script>
</pre>
</body>

You might also like