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

Modify the following code to evaluate the numerical derivative of sin(x) over th

ID: 3814846 • Letter: M

Question

Modify the following code to evaluate the numerical derivative of sin(x) over the interval x1 < x < x2. The user needs to input x1, x2, and h. The program also needs to employ a function called forward_derivative() as well as function_sin().

The forward derivative equation is as follows:

Dhf(x) = ((f(x + h) - f(x))/h)

/*-----------------------------------------------------------------*/
/* Program chapter6_12 */
/* */
/* This program estimates the area under a given curve */
/* using trapezoids with equal bases. */
#include<iostream> //Required for cin, cout
#include<cmath> //Required for exp()
using namespace std;
// Function prototypes.
double integrate(double a, double b, int n);
double f(double x);
int main()
{
// Declare objects
int num_trapezoids;
double a, b, area;
// Get input from user.
cout << "Enter the interval endpoints, a and b ";
cin >> a >> b;
cout << "Enter the number of trapezoids ";
cin >> num_trapezoids;
// Estimate area under the curve of 4e^-x
area = integrate(a, b, num_trapezoids);
// Print result.
cout << "Using " << num_trapezoids
<< " trapezoids, the estimated area is "
<< area << endl;
return 0;
}
/*-----------------------------------------------------------------*/
/*-----------------------------------------------------------------*/
double integrate(double a, double b, int n)
{
// Declare objects.
double sum(0), x, base, area;
base = (b-a)/n;
for(int k=2; k<=n; k++)
{
x = a + base*(k-1);
sum = sum + f(x);
}
area = 0.5*base*(f(a) + 2*sum + f(b));
return area;
}
double f(double x)
{
return(4*exp(-x));
}
/*-----------------------------------------------------------------*/

Explanation / Answer

ANSWER::[MAIN CODE]

#include<iostream>
using namespace std;

#include<math.h>

int main()

{ int Coeficient[100],i,liber[100],g,j,ajutor,w,help,h,m;

float x;

char Functie[100],Nou[100];
cout<<"Introduceti o functie:"<<endl;

cin>>Functie;

g=strlen(Functie);

j=0;

for(i=0;i<=g;i++)

{
if( (Functie[i]=='4') || (Functie[i]=='1') || (Functie[i]=='2') || (Functie[i]=='3') || (Functie[i]=='5')
       || (Functie[i]=='6') || (Functie[i]=='7') || (Functie[i]=='8') || (Functie[i]=='9') )
{

h=i;

do{
h++;

}
   while( (Functie[h]=='4') || (Functie[h]=='1') || (Functie[h]=='2') || (Functie[h]=='3') || (Functie[h]=='5')
       || (Functie[h]=='6') || (Functie[h]=='7') || (Functie[h]=='8') || (Functie[h]=='9') );
  
       {
if( ( (i == 0) || (Functie[i-1]=='(') || (Functie[i-1]=='+') || (Functie[i-1]=='-') ) &&
           ( (Functie[h]==')') || (Functie[h]=='+') || (Functie[h]=='-') || (Functie[h]==NULL) ) )

{

Nou[j]='0';

i=h-1;

}

else

Nou[j]=Functie[i];

}
if(Functie[i]=='+')

{

Nou[j]=Functie[i];

}
  
if(Functie[i]=='(')

{

Nou[j]=Functie[i];

}

if(Functie[i]==')')

{

Nou[j]=Functie[i];

}
if( (Functie[i]=='s') && (Functie[i+1]=='i') && (Functie[i+2]=='n') )

{

Nou[j]='c';

j++;

Nou[j]='o';

j++;

Nou[j]='s';

if(Functie[i+3]=='(')

{
j++;

Nou[j]='(';

help=i+4;

while(Functie[help]!=')')

{

j++;

Nou[j]=Functie[help];

help++;
}

j++;

Nou[j]=')';
j++;

Nou[j]='*';
}

else
{
j++;

Nou[j]=Functie[i+3];

j++;

Nou[j]='*';
}

i++;

i++;

}

if( (Functie[i]=='l') && (Functie[i+1]=='n') )

{

Nou[j]='(';

j++;

Nou[j]='1';

j++;

Nou[j]='/';

if(Functie[i+2]=='(')

{

j++;

Nou[j]='(';

help=i+3;

while(Functie[help]!=')')

{

j++;

Nou[j]=Functie[help];

help++;

}

j++;

Nou[j]=')';

j++;

Nou[j]=')';

j++;

Nou[j]='*';

}

else

{

j++;

Nou[j]=Functie[i+2];

j++;

Nou[j]=')';

j++;

Nou[j]='*';

}

i++;

}

if( (Functie[i]=='c') && (Functie[i+1]=='o') && (Functie[i+2]=='s') )

{
Nou[j]='-';

j++;

Nou[j]='s';

j++;

Nou[j]='i';

j++;

Nou[j]='n';

if(Functie[i+3]=='(')

{

j++;

Nou[j]='(';

help=i+4;

while(Functie[help]!=')')

{

j++;

Nou[j]=Functie[help];

help++;

}

j++;

Nou[j]=')';
j++;

Nou[j]='*';

}

else

{
j++;

Nou[j]=Functie[i+3];

j++;

Nou[j]='*';

}

i++;

i++;

}

if(Functie[i]=='-')
{

Nou[j]=Functie[i];

}

if(Functie[i]=='/')

{

Nou[j]=Functie[i];

}

if(Functie[i]=='*')

{

Nou[j]=Functie[i];

}

if(Functie[i]=='x')

{

if(Functie[i+1]=='^')

Nou[j]=Functie[i];

else

Nou[j]='1';

}

if(Functie[i]=='^')

{

Nou[j]=Functie[i];

Coeficient[i]=Functie[i+1]-'0'-1;

ajutor=Coeficient[i]+1;

Functie[i+1]=(char)(((int)'0')+Coeficient[i]);

j++;

Nou[j]=Functie[i+1];

j++;
Nou[j]='*';

j++;

Nou[j]=(char)(((int)'0')+ajutor);

i++;

}

j++;
}
cout<<"Functia este : "<<endl;
for(i=0;i<j-1;i++)
cout<<Nou[i];
cout<<endl;
system("PAUSE");
return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote