f(x) = sin(3x)/x elementof [-infinity, infinity] using N of different values, wh
ID: 3805607 • Letter: F
Question
f(x) = sin(3x)/x elementof [-infinity, infinity] using N of different values, where N is the number of steps or intervals. Print out all x_i, f_i = f(x_i) and the final result. Determine and print the appropriate value of N based on the results for different N values. Integrate the function by using both Trapezoidal rule and Simpson's rule, then compare the results with it calculated from mathematical software such as Matlab, Mathematica etc. Your C-code must contain the following features: (1) use of arrays, (2) use of pointers, (3) use of structure, (4) use of union, (5) use of functions and function calls, (6) formatted output on screen and (7) saving of the same output on a data file.Explanation / Answer
MATLAB Program For Simpson’s 1/3rd Rule
=============================================
%Created by myclassbook.org ()
%Created on 30 march 2017
clc;
clear all;
close all;
f=@(x)x^4; %Change here for different function
a=-3;b=3; %Given limits
n=b-a; %Number of intervals
h=(b-a)/n;
p=0;
for i=a:b
p=p+1;
x(p)=i;
y(p)=i^4; %Change here for different function
end
l=length(x);
x
y
answer=(h/3)*((y(1)+y(l))+2*(y(3)+y(5))+4*(y(2)+y(4)+y(6)))
trapezoidal rule
=========================
function I = trapezoidal(f_str, a, b, n)
I=0;
g = inline(f_str);
h = (b-a)/n;
I = I + g(a);
for ii = (a+h):h:(b-h)
I = I + 2*g(ii);
end
I = I + g(b);
I = I*h/2;
Code for TRAPEZOIDAL RULE in C Programming
===========================================
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x[10],y[10],sum=0,h,temp;
int i,n,j,k=0;
float fact(int);
clrscr();
printf(" how many record you will be enter: ");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf(" enter the value of x%d: ",i);
scanf("%f",&x[i]);
printf(" enter the value of f(x%d): ",i);
scanf("%f",&y[i]);
}
h=x[1]-x[0];
n=n-1;
for(i=0;i<n;i++)
{
if(k==0)
{
sum = sum + y[i];
k=1;
}
else
sum = sum + 2 * y[i];
}
sum = sum + y[i];
sum = sum * (h/2);
printf(" I = %f ",sum);
getch();
}
Code for SIMPSON'S 1/3 RULE in C Programming
==================================================
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x[10],y[10],sum=0,h,temp;
int i,n,j,k=0;
float fact(int);
clrscr();
printf(" how many record you will be enter: ");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf(" enter the value of x%d: ",i);
scanf("%f",&x[i]);
printf(" enter the value of f(x%d): ",i);
scanf("%f",&y[i]);
}
h=x[1]-x[0];
n=n-1;
sum = sum + y[0];
for(i=1;i<n;i++)
{
if(k==0)
{
sum = sum + 4 * y[i];
k=1;
}
else
{
sum = sum + 2 * y[i];
k=0;
}
}
sum = sum + y[i];
sum = sum * (h/3);
printf(" I = %f ",sum);
getch();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.