C Programming Differentiation HELP! IT'S DUE TODAY!

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.
Have you done anything? Can you post what you've done? We can only help, we can't do it for you.
this is what i have for the integration but i do not know how to change it so that it gets what i have posted above:


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

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();
}

Um... so you want a program to do numerical differentiation, right? Not symbolic? If the former is true, have you seen this or anything similar to this: http://en.wikipedia.org/wiki/Numerical_differentiation#Finite_difference_formulae ?

I suspect this will help you greatly.

-Albatross
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.
Look carefully at the first, third, and fourth formulae on that part of the Wiki page. Do you see any limits? *hint hint*

-Albatross
well it does not include it on those formulas but for the real one to find the derivative it is the first one but the limit is as h approaches 0.
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?

-Albatross
oh ok i see what you are saying now. alright but how do i actually change my integral coding to make it derivative? would it be something like this:


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

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.

-Albatrossleepy
Topic archived. No new replies allowed.