Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming Language C - Numerical Integration In this assignment you will write

ID: 3558504 • Letter: P

Question

Programming Language C - Numerical Integration

In this assignment you will write a small C program to read the coefficients of a third degree equation, and use a numerical integration method for calculating a numerical value of a definite integral in the range x= a to x=b. The C program should be submitted as a standard C source code file. You can submit your files separately or zipped together into one file. Please note that the computer program should comply with the commenting and formatting rules as has been done in class. For example, there should be a header for the whole program that gives the author's name, class name, date, and description. End braces should be commented, and there are alignment and indenting requirements as discussed. Please ask if you have any questions. A third degree polynomial equation is represented as f(x) = mx3 + nx2 + px + q .Where m, n, p and q are real values. f(x) = mx3 + nx2 + px + q; m,n,p,q R Numerical integration consists of finding numerical approximation for the value S in Fig. 1. To find the value of S, you will implement and compare two different solutions: Midpoint rule: Also known as the rectangle rule; to find the rectangle area (S), the base is the difference (b-a) and the height is the result of evaluating f(x) when x is the midpoint between a and b: f(x)dx (b - a) f .(a+b/2). Trapezoidal rule: Similar to but in this case the height of the rectangle, requires to evaluate f(x) at x = a and x = b. The height is then computed as the average of these two measurements: f(x)dx (b - a)f(a) + f(b)/2. Assignment: Your first program is a simple mathematical calculation. You will write a C program that shows a menu to get 4 real values from the user, namely m, n, p and q(20 points); once these values are provided, the user can enter the integration range(i.e. the values for a and b) (10 points). A third option will calculate the value of S, using methods 1 (midpoint) and 2(trapezoidal) explained above(40 points) and display the results as well as the difference between the value of Si obtained with method 1, and the value of Si obtained with method 2. Your program should prompt the user for these values (coefficients and range), accept input from the keyboard, and then print out the result. You can use a menu with the following options: Enter new coefficients (m, n, p and q) Enter the value for a and b Calculate the integral from a to b Exit Include a report in a Word file that shows the results for the following values: Coefficients (-1, 4,-5, 2) and range (0,5) (5 points) Coefficients (1, 5, 7, 2) and range (-2.5,1) (5 points) Coefficients (1, 0, -1, 8) and range (-1,1) (5 points) Coefficients (1/4, 3/4, -6/4, -2) and range (-4,2) (5 points) Add your observations (10 points): * Optional [Extra Points] : Include the plots for i) to iv), using MATLAB (10 points). Compute the error between the value for S obtained with MATLAB (Sm) and methods 1 and 2 (Si and S? respectively)(10 points). If you do not have access to MATLAB you can also use OCTAVE or SciLab. You are allowed to use the function float pow( float, int), found in , to

Explanation / Answer

Note I have used step size as 1000 for integration so error is less for me . if you want error(s1-s2 to be more , put less step size that is change h = (b-a)*0.001 to may be (b-a)*0.1 )

C Code :

#include <stdio.h>
#include <string.h>
#include <math.h>
//your details goes here
main()
{   
double m,n,p,q,a,b,S1,S2,hmid,diff;
double h,x[1001],y[1001],ymid;
int i ;
printf("Enter new coefficients (m,n,p and q):");   
scanf("%lf %lf %lf %lf",&m,&n,&p,&q);
printf(" Enter the value for a and b: ");
scanf("%lf %lf",&a,&b);

h= (b-a)*0.0001000;

i=1;
S1=0;
S2=0;
x[0]=a;
y[0]=m*pow(a,3)+n*pow(a,2)+p*pow(a,1)+q;

while (i<=1000)
{
   x[i]=a+i*h;
     
   y[i]=m*pow(x[i],3)+n*pow(x[i],2)+p*pow(x[i],1)+q ;
   hmid = (x[i]+x[i-1])/2;
   ymid=m*pow(hmid,3)+n*pow(hmid,2)+p*pow(hmid,1)+q;
   S1=S1+(h)*(ymid);
   S2 =S2+h*((y[i]+y[i-1])/2);
     
i=i+1;
}
printf("Area Calculated by Method1 is S1= %lf ",S1);
printf("Area calculated by Method2 is S2 = %lf ",S2);
diff= S1-S2;
printf(" Diffrence in area by method1 and method2 is S1-S2=%lf ",diff);

}

Outputs for given Cases

1. S1 = 0.192708

S2 = 0.192708

S1-S2 = 0

2. S1 = 0.05771 , S2 = 0.05771 , S1-S2 = 0

3.S1= 1.6324 , S2 = 1.6324 , S1-S2 = 0

4.S1 = 0.6561 , S2 = 0.6561 , S1-S2 = 0

Observations : As I have divided the area into 1000 small parts(large no of chunks) , the 2 methods have shown no difference in results

in the iv) part we have to enter the decimal number not the fractions for the double to take that value .

Hope this helps . Please do rate positively :D)