






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Examples of numerical methods for solving equations, including the bisection method, newton-raphson method, and height calculation using these methods. The examples include solving for x in cos(2x) + sin(2x) + x - 1, solving for t in ln osf, and calculating molar volume. The methods are demonstrated using java code.
Typology: Study notes
1 / 12
This page cannot be seen from the preview
Don't miss anything!







double xl, xu, xm, fxl, fxu, fxm, fx; int k; System.out.print("Enter the value of xl: "); xl=input.nextDouble(); System.out.print("Enter the value of xu: "); xu=input.nextDouble(); k=1; fxl=Math.cos(2xl)+Math.sin(2xl)+xl-1; fxu=Math.cos(2xu)+Math.sin(2xu)+xu-1; System.out.print("k \t xl \t xu \t xm \t fx \t fxm\n");
if (((fxl>0) && (fxu<0)) || ((fxl<0) && (fxu>0))) { xm=(xl+xu)/2; fxm=Math.cos(2xm)+Math.sin(2xm)+xm-1; while ((Math.abs(fxm))>=0.00001) { fxl=Math.cos(2xl)+Math.sin(2xl)+xl-1; xm=(xl+xu)/2; fxm=Math.cos(2xm)+Math.sin(2xm)+xm-1; fx = fxm*fxl; System.out.printf("%d \t %.4f \t %.4f \t %.4f \t %.4f \t %.4f \n", k, xl, xu, xm, fx, fxm); if (fx<0) { xu=xm; xl=xl; k++; } else { xu=xu; xl=xm; k++; } } System.out.printf ("The root of the equation is %.4f\n",xm); } else { System.out.print("No roots of the equation\n"); main (args); }
double x, xn, fx, f2x; int k; System.out.print("Enter the initial value of x:"); x = input.nextDouble(); k = 1; fx = Math.cos(2x)+Math.sin(2x)+x-1; System.out.print("k \t x \t xn \t fx \n"); while (Math.abs(fx)>=0.00001) { fx = Math.cos(2x)+Math.sin(2x)+x-1; f2x =(-2Math.sin(2x))+(2Math.cos(2x))+1; xn = x-(fx/f2x); x = xn; k++; System.out.printf("%d \t %.4f \t %.4f \t %.4f \n", k, x, xn, fx ); } System.out.printf("The root of the equation is %.4f\n", x);
else { if (choice ==2 ) { System.out.println("Please enter estimated height"); h = input.nextDouble(); System.out.println("k\t h\t fh\t"); fh = Math.pow(h,3) - 9*Math.pow(h,2) + 3.8197;
while (Math.abs(fh) >= tolerance) { fh = Math.pow(h,3) - 9Math.pow(h,2) + 3.8197; f2h = 3Math.pow(h,2) - 18*h; System.out.printf("%d\t %5.4f %5.4f\n", k, fh, f2h); h = h - (fh/f2h); k++; } System.out.printf("The calculated height in ft is %5.4f", h); } else { System.out.println("Please choose from 1 and 2 only"); main (args); } }
double Ta, Tn, T, Osf, A, B, C, D, fT, fT1, Tb, Tc, Td; double A1, B1, C1, D1; int k;
k=1; System.out.println("Enter the assumed temperature in °C:"); Ta=input.nextDouble(); System.out.println("Enter the DO in mg/L:"); Osf=input.nextDouble();
if (Ta>=0 && Ta<=40) { T=Ta+273.15; A = (1.575701Math.pow(10, 5))/T; B = (6.642308Math.pow(10, 7))/(Math.pow(T,2)); C = (1.2438Math.pow(10, 10))/Math.pow(T, 3); D = (8.621949Math.pow(10,11))/Math.pow(T, 4);
fT=-139.34411+A-B+C-D-Math.log(Osf); System.out.print("k \tTa(°C) \tTn(°C) \t fT \tfT'\n");
while (Math.abs(fT)>=0.0001) { A = (1.575701Math.pow(10, 5))/T; B = (6.642308Math.pow(10, 7))/(Math.pow(T,2)); C = (1.2438Math.pow(10, 10))/Math.pow(T, 3); D = (8.621949Math.pow(10,11))/Math.pow(T, 4); fT=-139.34411+A-B+C-D-Math.log(Osf);
A1 = -(1.575701Math.pow(10, 5))/Math.pow(T,2); B1 = (26.642308Math.pow(10, 7))/(Math.pow(T,3)); C1 = -(31.2438Math.pow(10, 10))/Math.pow(T, 4); D1 = (48.621949*Math.pow(10,11))/Math.pow(T, 5); fT1=A1+B1+C1+D1;//derivative
Tn=T-(fT/fT1); Tc=T-273.15; //To convert the output in the table into degrees Celsius Td=Tn-273.15; System.out.printf("%d \t%.5f \t %.5f \t%.5f \t%.5f\n", k, Tc, Td, fT, fT1); T=Tn; k++; }
Tb=T-273.15;//To convert the output in the table into degrees Celsius System.out.printf("The value of Ta in °C is %.5f", Tb);
} else { System.out.println("Enter the temperature within 0-40°C"); main (args); }
else { xu=xm; } k++; } while (Math.abs(fxm)>=0.00001); System.out.printf("The molar volume in cubic is %10.4f",xm); } else { System.out.print("Please enter a valid value of the limits"); } } else { if (choice==2) { System.out.print("Enter the initial estimate of the molar volume in cm^3: "); x = input.nextDouble();
k=1; do { fx = ((RT)/(x-b))-((a)/(Math.pow(T,0.5)x(x+b)))-P; f2x = (RT)/Math.pow((x-b),2)-(a((Math.pow(T,0.5)2x)+(Math.pow(T,0.5)Math.pow(T, 0.5)*b))); x = x-(fx/f2x); k++; } while (Math.abs(fx)>=0.00001); System.out.printf("The molar volume in cubic is %10.4f",x); } else { System.out.print("Please enter a valid choice"); } }
Scanner input = new Scanner (System.in); double xl, xu, fxl, fxu, xm, fxm, osf, fx;
System.out.print("Enter the value of the dissolved oxygen in mg/L: "); osf = input.nextDouble();
xl = 0+273.15; xu = 40+273.15;
fxl = -139.34411+((1.575701Math.pow(10,5))/xl)-((6.642308Math.pow(10,7))/Math.pow(xl,2))+((1. 38Math.pow(10,10))/Math.pow(xl,3))-((8.621949Math.pow(10,11))/Math.pow(xl,4))-Math.log(osf ); fxu = -139.34411+((1.575701Math.pow(10,5))/xu)-((6.642308Math.pow(10,7))/Math.pow(xu, 2))+((1.2438Math.pow(10,10))/Math.pow(xu,3))-((8.621949Math.pow(10,11))/Math.pow(xu,4))- Math.log(osf);
System.out.print(" xl\t xu\t fxl\t fxu\t xm\t fxm\t osf\n"); if ((fxl>0&&fxu<0)||(fxl<0&&fxu>0)) { do { fxl = -139.34411+((1.575701Math.pow(10,5))/xl)-((6.642308Math.pow(10,7))/Math.pow(xl,2))+((1. 38Math.pow(10,10))/Math.pow(xl,3))-((8.621949Math.pow(10,11))/Math.pow(xl,4))-Math.log(osf ); xm = (xu+xl)/2; fxm = -139.34411+((1.575701Math.pow(10,5))/xm)-((6.642308Math.pow(10,7))/Math.pow(xm,2))+((1. 2438Math.pow(10,10))/Math.pow(xm,3))-((8.621949Math.pow(10,11))/Math.pow(xm,4))-Math.lo g(osf); fx = fxm*fxl;
if (fx>0) { xl=xm; } else { xu=xm; }