Alright so I have to create a program that calculates the derivative or the slope of an arbitrary input function with arbitrary duration and limits. You have to start calculating at at least the second data point.
You do not have to make it so that you can enter any function and it can do it. It can be for example you just come up with a random like y=10x and the program would just be able to calculate it for that function if that makes sense.
I created a program for integration using tutorials and stuff but I could not figure it out for differentiation and it is due in 9 hours for me so whoever can help as soon as possible I would greatly appreciate it.
float f(float x)
{return cos(x); //this is the function that gets integrated
}
int ourFunction(int,FILE *);
int main(void){
float x; //defines x as a float
float a,b,h; //defines a,b,h as a float
float integral;
int n; //number of divisions
int i; //dummy variable for counting
i=1; //i starts at 1
printf("\nEnter the lower limit of the integral:"); //this is where you type the lower bound
scanf("%f",&a);
printf("\nEnter the upper limit of the integral:"); //this is where you type the upper bound
scanf("%f",&b);
printf("\nEnter desired number of rectangles(more rectangles, more accurate result):"); //the number of rectangles you want the area to be calculated
scanf("%d",&n);
//the actual calculations
h=(b-a)/n;
integral=(f(a)+f(b))/2;
while (i<n){
integral+=f(a+i*h);
i++;
}
integral=integral*h;
printf("\nThe integral of f(x) from a=%3f to b=%3f is %f",a,b,integral);
getchar(); getchar();
}
yes numerical. I don't know how I would put that into the calculations because part of finding the derivative it's also finding the limit as h approaches 0. If someone can edit my code to be able to do it or direct to a place that I can get help on the specific code it would be great.
On the formula for a (symbolic) derivative, yes. But what I understand is that you want a formula that you can use to get an estimate, correct? One similar in purpose and use to your integral formula?
float f(float x)
{return cos(x); //this is the function that gets integrated
}
int ourFunction(int,FILE *);
int main(void){
float x; //defines x as a float
float a,b,h; //defines a,b,h as a float
float derivative;
int n; //number of divisions
int i; //dummy variable for counting
i=1; //i starts at 1
printf("\nEnter the lower limit of the integral:"); //this is where you type the lower bound
scanf("%f",&a);
printf("\nEnter the upper limit of the integral:"); //this is where you type the upper bound
scanf("%f",&b);
printf("\nEnter desired number of rectangles(more rectangles, more accurate result):"); //the number of rectangles you want the area to be calculated
scanf("%d",&n);
//the actual calculations
h=(b-a)/n;
derivative=(f(a+h)-f(a))/h;
while (i<n){
integral+=f(a+i*h);
i++;
}
integral=integral*h;
printf("\nThe integral of f(x) from a=%3f to b=%3f is %f",a,b,integral);
getchar(); getchar();
}
The way I see it, your program works in two steps. Bear with me, I'm a little bit tired at the moment.
1. Ask for information from the user. What you need to know is where you want to figure out the slope (your x), and second (if you want to give such an option) is your h (tell the user that the smaller h is, the more accurate the result will be).
2. Calculate the derivative. I suggest scrapping all he code that you had for the integral calculations and implementing the derivative code from scratch.