Numerical Methods for Solving Equations, Study notes of Computer Applications

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

2023/2024

Uploaded on 04/17/2024

deejhay-kharl-galvan-1
deejhay-kharl-galvan-1 🇵🇭

1 document

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Cos2x + Sin2x + x-1 (HIM)
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(2*xl)+Math.sin(2*xl)+xl-1;
fxu=Math.cos(2*xu)+Math.sin(2*xu)+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(2*xm)+Math.sin(2*xm)+xm-1;
while ((Math.abs(fxm))>=0.00001)
{
fxl=Math.cos(2*xl)+Math.sin(2*xl)+xl-1;
xm=(xl+xu)/2;
fxm=Math.cos(2*xm)+Math.sin(2*xm)+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);
}
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Numerical Methods for Solving Equations and more Study notes Computer Applications in PDF only on Docsity!

Cos2x + Sin2x + x-1 (HIM)

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); }

Cos2x + Sin2x + x-1 (NRT)

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); } }

Solving for T in ln OSF (HIM)

• user will input T and DO

double Tl, Tu, Tm, T1, T2, T3, Osf, A, B, C, D, A2, B2, C2, D2, fT;

double fT1, fT2, fTm, Tb;

double A3, B3, C3, D3;

int k;

k=1;

System.out.println("Enter the lower temperature in °C:");

Tl=input.nextDouble();

System.out.println("Enter the upper temperature in °C:");

Tu=input.nextDouble();

System.out.println("Enter the DO in mg/L:");

Osf=input.nextDouble();

if (Tl>=0 && Tl<=40 && Tu>=0 && Tu<=40)

T1=Tl+273.15;

A = (1.575701*Math.pow(10, 5))/T1;

B = (6.642308*Math.pow(10, 7))/(Math.pow(T1,2));

C = (1.2438*Math.pow(10, 10))/Math.pow(T1, 3);

D = (8.621949*Math.pow(10,11))/Math.pow(T1, 4);

T2=Tu+273.15;

A2 = (1.575701*Math.pow(10, 5))/T2;

B2 = (6.642308*Math.pow(10, 7))/(Math.pow(T2,2));

C2 = (1.2438*Math.pow(10, 10))/Math.pow(T2, 3);

D2 = (8.621949*Math.pow(10,11))/Math.pow(T2, 4);

fT1=-139.34411+A-B+C-D-Math.log(Osf);

fT2=-139.34411+A2-B2+C2-D2-Math.log(Osf);

System.out.print("k \tT1(°C) \tT2(°C) \t Tm \tfT1 \tfT2 \fTm\n");

if((fT1>0&&fT2<0)||(fT1<0&&fT2>0))

Tm=(Tl+Tu)/2;

T3=Tm+273.15;

A3 = (1.575701*Math.pow(10, 5))/T3;

B3 = (6.642308*Math.pow(10, 7))/(Math.pow(T3,2));

C3 = (1.2438*Math.pow(10, 10))/Math.pow(T3, 3);

D3 = (8.621949*Math.pow(10,11))/Math.pow(T3, 4);

fTm=-139.34411+A3-B3+C3-D3-Math.log(Osf);

while (Math.abs(fTm)>=0.0001)

Solving for T in ln OSF (NRT)

• user will input T and DO

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"); } }

Solving for T in ln OSF (HIM)

• user will input DO

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; }