MAE2360 FINAL EXAMINATION
1-2:20pm August 16, 2021

For Problems 1-4, you need to list (1) your C (Octave/Matlab for Prob.2) code and (2) an example run.
2


1. Write a C program to compute the cube root of 12 (=3√{12}) using the Newton-Raphson method.


2. Translate the following C code to an Octave/MATLAB script m-file:

#include <stdio.h>
#include <math.h>

double f(double x)
 {return 4.0/(1.0+x*x);}

int main()
{
 int i, n;
 double a=0.0,b=1.0,h,s1=0.0,
  s2=0.0,s3=0.0,x;

 printf("Enter # of partitions = ");
 scanf("%d", &n);

 h=(b-a)/(2.0*n);

 s1=(f(a)+ f(b));

 for(i=1;i<2*n;i=i+2)s2=s2+f(a+i*h);

 for(i=2;i<2*n;i=i+2)s3=s3+f(a+i*h);

 printf("%f\n",(h/3)*(s1+4*s2+2*s3));

 return 0;
}

You can use f=@(x) 4/(1+x*x); as an inline (anonymous) function (or you can use a separate function-m file).


3. Find y(2) for
y′(t) = y

t+y2
,
with y(1)=1 and h=0.05 using the Runge-Kutta method.











4. Solve the following set of nonlinear equations by the Gauss-Seidel method.
x sin y + 60 y +ez
=
2,
30 x + x y zz sin y
=
1,
x z+cos y+40z
=
3.
Start with an initial guess of x = y = z=0.




5. TRUE or FALSE

    (a) Gauss proved that there is no closed formula available for the solution of fifth-order algebraic equations and beyond.
    (b) The Simpson method is the default scheme for solving differential equations numerically.
    (c) To enter a long line ( > 80 characters) in FORTRAN, the CONTINUE statement can be used.
    (d) A file name such as
    hw-11.m
    
    is not acceptable in Octave/MATLAB.
    (e) The indefinite integral of [1/(x2)] is log (x2).
Solution
1.
#include <stdio.h>
#include <math.h>
#define EPS 1.0e-10

double f(double x)
{
	return pow(x,3)-12;
}

double fp(double x)
{
	return 3*x*x;
}

double newton(double x)
{
	return x - f(x)/fp(x);
}

int main()
{
	double x1, x2;
	int i;

	printf("Enter initial guess  =");
	scanf("%lf", &x1);

	if (fp(x1)==0.0) {
		printf("No convergence.\n");
		return 0;
	}

	for (i=0;i<100;i++)
	{
		x2=newton(x1);
		if (fabs(x1-x2)< EPS) break;
		x1=x2;
	}

	printf("iteration = %d\n", i);
	printf("x= %lf\n", x1);
	return 0;
}

C:\gcc-2.95.2>a
Enter initial guess  =2
iteration = 4
x= 2.289428

C:\gcc-2.95.2>a
Enter initial guess  =3
iteration = 5
x= 2.289428

2.
f=@(x) 4/(1+x*x);
a=0.0; b=1.0; s1=0.0; s2=0.0; s3=0.0;
n=input("Enter number of partitions  = ");
 h = (b-a)/(2.0*n) ;
 s1 = (f(a)+ f(b));
 for i=1:2:2*n-1   s2 = s2 + f(a + i*h); end;
 for i=2:2:2*n-1  s3 = s3 + f(a + i*h); end;
 fprintf("%f\n", (h/3.0)*(s1+ 4.0*s2 + 2.0*s3)) ;

$ octave
octave:1> prob2
Enter number of partitions  = 10
3.141593
octave:2> prob2
Enter number of partitions  = 6
3.141593
octave:3>


3.

#include <stdio.h>
#include <math.h>

double f(double t, double y)
{return y/(t+y*y);}

int main()
{
double h=0.05, t, y, k1,k2,k3,k4;
int i;

/* initial value */
t=1.0; y=1.0;

for (i=0; i<=20; i++)
{

 printf("t= %lf rk= %lf\n", t, y);
 k1=h*f(t,y);
 k2=h*f(t+h/2, y+k1/2.0);
 k3=h*f(t+h/2, y+k2/2.0);
 k4=h*f(t+h, y+k3);
 y= y+(k1+2.0*k2+2.0*k3+k4)/6.0;
 t=t+h;
}
return 0;
}

$ gcc 2.c -lm
$ ./a.out
t= 1.000000 rk= 1.000000
t= 1.050000 rk= 1.024695
t= 1.100000 rk= 1.048809
t= 1.150000 rk= 1.072381
t= 1.200000 rk= 1.095445
t= 1.250000 rk= 1.118034
t= 1.300000 rk= 1.140175
t= 1.350000 rk= 1.161895
t= 1.400000 rk= 1.183216
t= 1.450000 rk= 1.204159
t= 1.500000 rk= 1.224745
t= 1.550000 rk= 1.244990
t= 1.600000 rk= 1.264911
t= 1.650000 rk= 1.284523
t= 1.700000 rk= 1.303840
t= 1.750000 rk= 1.322876
t= 1.800000 rk= 1.341641
t= 1.850000 rk= 1.360147
t= 1.900000 rk= 1.378405
t= 1.950000 rk= 1.396424
t= 2.000000 rk= 1.414214

4.

#include <stdio.h>
#include <math.h>
int main()
{
double x, y, z;
int i,n;

x=y=z=0.0;

printf("Enter # of iteration = ");
scanf("%d", &n);

for (i=0;i<n;i++)
{
x = (1+z*sin(y)-x*y*z)/30;
y = (2-x*sin(y)-exp(-z))/60;
z = (3-x*z-cos(y))/40.;
}

printf("x = %lf, y= %lf, z=%lf\n", x,y,z);
return 0;
}

$ gcc 3.c -lm
$ ./a.out
Enter # of iteration = 2
x = 0.033360, y= 0.017470, z=0.049962
$ ./a.out
Enter # of iteration = 3
x = 0.033361, y= 0.017469, z=0.049962
$ ./a.out
Enter # of iteration = 4
x = 0.033361, y= 0.017469, z=0.049962

5.

    (a)F. It's Galois, not Gauss.
    (b)F Engineering/Scientific sectors.
    (c)F Put any character at the 6th column.
    (d)T It's interpreted as hw minus 11.
    (e)F



File translated from TEX by TTH, version 4.03.
On 18 Apr 2024, 23:23.