Thanks a lot people for your help so far but I made a big mistake I need the derivation of a function at a specific point!
I have to calculate the first derivation of a function and I really have no clue how to get there. If I just had to calculate it for a function with just a X^1 I would know how to but I'm really stuck here.
Old Stuff:
A function can look like 2*x^2+1.
The method has to look like this: double ab(double (f)(double),double x)
and my professor gave us the hint that we might should use the function:
(f(x0+∆x)−f(x0))/((x0+∆x)−x0).
Sorry for my bad English and thanks for any kind of hint or tip in advance.
this sample will get you started :
#include<stdio.h>
#include <stdlib.h>
float func(float x)
{
return(2*x*x + 1);
}
int main(){
float h=0.01;
float x;
float deriv, second;
printf("Enter x value: ");
scanf("%f", &x);
// derivative at x is the slope of infinitely small
// line of the function
deriv = (func(x+h) - func(x))/h; // I assumed the length to be h
//for second derivative you can use:
second = (func(x+h) - 2*func(x) + func(x-h))/(h*h);
printf("%f\n", deriv);
return 0;
}
The idea is approximate the first derivative of f() at x with the slope of the secant line through the points (x, f(x)) and (x+∆x, f(x+∆x)).
The Wikipedia article should get you started.
Related
We are doing C Programming in school and we have to calculate the cosine of a number with use of the "Taylor series" (https://en.wikipedia.org/wiki/Taylor_series). I know C programming but that what we have to do has not really much to do with programming itself more than being good in math.
If you put in your calculator cos(50) it's 0.6427.... That's what we have to do in C. There is the cos() function in math.h but that's not actually the cosine. I am overwhelmed with this and don't really know what we have to do. It should look something like this:
#include <stdio.h>
#include <conio.h>
#include <math.h>
int fac(int n);
double meincosinus(double x);
void main() {
double x;
double y;
int i;
printf("geben sie eine zahl ein\n");
scanf_s("%lf", &x);
y = meincosinus(x);
printf_s("cos(%lf)=%lf\n", x, y);
y = sin(x);
printf_s("cos(%lf)=%lf\n", x, y);
scanf_s("%d", &i);
printf_s("%d\n", i);
_getch();
}
int fac(int n) {
int prod = 1;
int i;
for (i = 1; i <= n; i++)
{
prod *= i;
}
return prod;
}
double meincosinus(double x)
{
double erg = 0;
int n;
for (n = 0; n <= x; n++)
{
x = fac(n);
erg += pow(-1, n) * pow(x, (2 * n)) / fac(2 * n);
}
return erg;
}
This code runs but the output is wrong.
Not an answer (but, well, I am a teacher myself, I am not gonna make your homework :)). But a few questions you should ask yourself and that might help you
Why are you comparing your Taylor computation of cosinus with the "real" sinus? Shouldn't y=sin(x) be y=cos(x), for a pertinent comparison?
why are you doing this: x = fac(n);? You are overwriting your x argument to "meinconsinus". You can't expect meincosinus(50) to really compute cos(50) if the first thing you do is overwriting that "50" with something else (namely n!).
also, why this for (n = 0; n <= x; n++). You know the Taylor formula better that I do (since you are studying it right now). It sure not supposed to stop after x iterations. x is rarely even an integer. It is supposed to be very small anyway. And could even be negative. So, question you've to ask yourself is, how many iterations (up to which term of the series) you want to compute. It is a rather arbitrary choice, since from Taylor point of view, answer is ∞. But on a computer, after a while it is no use to add extremely small numbers
Since I am mentioning the fact that x is supposed to be small: you can't compute cos(50) that way with a Taylor formula. You could compute cos(0.1) or even cos(1) maybe, with enough iterations. But 50 is not a small enough number. Plus, 50ⁿ will be very quickly out of control in your loop. If you really want meincosinus to be able to handle any number, you have first to reduce x, using trigonometric rules: cos(x)=cos(x)-2π; cos(x)=-cos(x); cos(x)=sin(π/2-x); ... There are some better rules, but with those simple ones, you can have x in [0,π/4]. Since π/4<1, at least you don't have an explosive xⁿ.
Also, but that is an optimization, you don't really need to compute neither (-1)ⁿ with pow (just alternate a int sign variable, between -1 and 1 at each iteration), nor x²ⁿ (just multiply a double x2n=1 by x*x each iteration), nor fac(2n) (just multiply a f=1 variable by (n-1)×n, being careful with 0 case, at each iteration.
I have these two equations
and I need to convert them to C code where you inputk and x. The thing is I don't get that advanced levels of math, neither did I learn C in the past :D
Can anyone show me step by step what built-in functions can be used for this and how exactly should the logic behind the app work?
Cheers!
Your formula is wrong. As shown here (along with the proof of the derivation) the correct formula is
You have k and n swapped in your summation. The inputs should then be x and n. The correct code is then:
#include <math.h>
double sum_of_sin(double x, int n) {
if (sin(x/2) == 0.0) {
return 0.0; //prevent division by 0 for x multiple of 2π
}
return sin(n*x/2) * sin((n+1)*x/2) / sin(x/2);
}
You can include the file math.h which has inbuilt functions like cos() and sin().
For instance:
#include <stdio.h>
#include <math.h>
int main ()
{
double res;
ret = cos(45);
printf("The cos of angle 45 is %f",res);
return 0;
}
Hope it helps..
You have an equation that consists of two formulae. It is easy to write the two parts out, but it can be hard to find a solution. The right-hand part of the first formula would be:
float RHvalue;
RHvalue = (sin( ((n+1.0)*x) / 2.0 ) * sin(n * x/2.0) ) / sin(x/2.0);
now, what the values of kx would be, is another matter, though it would be easy to sum them.
So here is a program that is supposed to calculate an approximation to the value of pi if you take enough terms into the sum which is mathematically described in the following program and calculates the expression of the root, you get a value that gets closer and closer to the value of pi the more terms you have.
#include <stdio.h>
#include <math.h>
main()
{
int j, terms;
double sum, precision, pi;
printf("How many terms: "); scanf("%d", &terms);
for(j=1;j<=terms;j++)
sum+=1/(j*j);
pi = sqrt(6*sum);
printf("Pi: %lf.\n", pi);
}
But there is something making it go wrong here and I can't quite figure out what.
sum+=1/(j*j);
I thought the mistake might be in that line because all others look fine,thinking at first maybe the computer isn't counting decimals.I'm unsure.But my question is: What is it in this code that makes it malfunction?And how do I fix it?
This performs integer division:
1/(j*j);
try this:
sum+=1.0/(j*j);
If j*j might overflow, do this
sum+=1.0/((double)j*j);
I have a code, that was not made by me.
In this complex code, many rules are being applied to calculate a quantity, d(x). in the code is being used a pointer to calculate it.
I want to calculate an integral over this, like:
W= Int_0 ^L d(x) dx ?
I am doing this:
#define DX 0.003
void WORK(double *d, double *W)
{
double INTE5=0.0;
int N_X_POINTS=333;
double h=((d[N_X_POINTS]-d[0])/N_X_POINTS);
W[0]=W[0]+((h/2)*(d[1]+2.0*d[0]+d[N_X_POINTS-1])); /*BC*/
for (i=1;i<N_X_POINTS-1;i++)
{
W[i]=W[i]+((h/2)*(d[0]+2*d[i]+d[N_X_POINTS]))*DX;
INTE5+=W[i];
}
W[N_X_POINTS-1]=W[N_X_POINTS-1]+((h/2)*(d[0]+2.0*d[N_X_POINTS-1]+d[N_X_POINTS-2])); /*BC*/
}
And I am getting "Segmentation fault". I was wondering to know if, I am doing right in calculate W as a pointer, or should declare it as a simple double? I guess the Segmentation fault is coming for this.
Other point, am I using correctly the trapezoidal rule?
Any help/tip, will very much appreciate.
Luiz
I don't know where that code come from, but it is a lot ugly and has some limits hard-encoded (333 points and increment by 0.003). To use it you need to "sample" properly your function and generate pairs (x, f(x))...
A possible clearer solution to your problem is here.
Let us consider you function and let us suppose it works (I believe it does't, it's a really obscure code...; e.g. when you integrate a function, you expect a number as result; where's this number? Maybe INTE5? It is not given back... and if it is so, why the final update of the W array? It's useless, or maybe we have something meaningful into W?). How does would you use it?
The prototype
void WORK(double *d, double *W);
means the WORK wants two pointers. What these pointers must be depends on the code; a look at it suggests that indeed you need two arrays, with N_X_POINTS elements each. The code reads from and writes into array W, and reads only from d. The N_X_POINTS int is 333, so you need to pass to the function arrays of at least 333 doubles:
double d[333];
double W[333];
Then you have to fill them properly. I thought you need to fill them with (x, f(x)), sampling the function with a proper step. But of course this makes no too much sense. Already said that the code is obscure (now I don't want to try to reverse engineering the intention of the coder...).
Anyway, if you call it with WORK(d, W), you won't get seg fault, since the arrays are big enough. The result will be wrong, but this is harder to track (again, sorry, no "reverse engineering" for it).
Final note (from comments too): if you have double a[N], then a has type double *.
A segmentation fault error often happens in C when you try to access some part of memory that you shouldn't be accessing. I suspect that the expression d[N_X_POINTS] is the culprit (because arrays in C are zero-indexed), but without seeing the definition of d I can't be sure.
Try putting informative printf debugging statements before/after each line of code in your function so you can narrow down the possible sources of the problem.
Here's a simple program that integrates $f(x) = x^2$ over the range [0..10]. It should send you in the right direction.
#include <stdio.h>
#include <stdlib.h>
double int_trapezium(double f[], double dX, int n)
{
int i;
double sum;
sum = (f[0] + f[n-1])/2.0;
for (i = 1; i < n-1; i++)
sum += f[i];
return dX*sum;
}
#define N 1000
int main()
{
int i;
double x;
double from = 0.0;
double to = 10.0;
double dX = (to-from)/(N-1);
double *f = malloc(N*sizeof(*f));
for (i=0; i<N; i++)
{
x = from + i*dX*(to-from);
f[i] = x*x;
}
printf("%f\n", int_trapezium(f, dX, N));
free(f);
return 0;
}
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
Using two functions:
Factorial Function
Power Function
Develop a C program for the following equation:
I've done this . but no output.
#include <stdio.h>
#include <math.h>
double factorial(double x);
double Power_function(double y);
int main(){
double answer=0;
double n;
double y;
printf("Enter Y :The last limit of the summation:>");
scanf("%ld",&y);
for (n=1;n<=y;++n){
answer=answer +factorial(n)*Power_function(n)*y;
}
printf("The Answer is %0.2f\n",answer);
return 0;
}
double factorial(double x)
{
double ans;
if (x==0){
ans = 1;
}
else
{
ans = x*factorial(x-1);
}
return ans;
}
double Power_function(double y){
double ans;
ans=pow(2,y);
return ans;
}
I've a suspicion what one of the learning outcomes of this is and too much help here will give the game away.
I think i can safely say you need to break each part of the equation down to smaller solvable parts.
The three biggest parts are summation, N Factorial (N!) and 2 to the power N
The summation is effectively a loop with N starting at 1 and ending at y, so look for the C syntax to write a loop (hint there are two common types, while and for)
The other two are functions, if your allowed to use premade functions then plenty exist only a google away which will sort you out nicely, if not you'll have to write your own.
factorial is n*(n-1)*(n-2)...(n-(n-2))*(n-(n-1))
so 4! = 4*3*2*1
a prime candidate for a recursive function or a function with a descending loop in it
2 to the power n is 2 multiplied by itself n times
so 2 to the power 2 = 2*2
2 to the power 3 = 2*2*2 and so on
Once again a loop looks like a good place to start with that.
After that its just a matter of using your two functions inside the loop, giving the user a way to input Y and testing with some numbers.
1,2,3 would be a good start as they're nice and easy to work out on a calculator.
All well and good so far I'd hope, -1 should make interesting things happen to every example I've seen posted so far, and that's before we start pushing the boat out with big numbers like 32.
Edit:
Right, I've taken a look at your code, and it looks like your problem isn't in the implementation of your algorithm, its not reading the variable in correctly, outputing y just after you set it yields 0, so something isn't working quite right there. Im not a c coder but i had a quick hack at it with some liberal googling and your non functional code and made it read an argument in then parse it to a double.
#include<stdio.h>
#include<math.h>
double factorial(double x);
double Power_function(double y);
main(int argc, char *argv[]){
double answer=0;
double n;
double y;
y = atol(argv[1]);
//printf("%lf\n",y);
for (n=1;n<=y;++n){
answer=answer +factorial(n)*Power_function(n)*y;
}
printf("The Answer is %0.2f\n",answer);
return 0;
}
double factorial(double x)
{
double ans;
if (x==0){
ans = 1;
}
else
{
ans = x*factorial(x-1);
}
return ans;
}
double Power_function(double y){
double ans;
ans=pow(2,y);
return ans;
}
So essentially your code with a different input method,
compiled using
gcc so.c -lm
executed as
./a.out 1
yields The Answer is 2.00
./a.out 2
yields The Answer is 20.00
./a.out 3
yields The Answer is 174.00
Pen and paper maths backs it up so your algorithm is sound so far!
Feed it some negative numbers and some huge numbers to see what it does from here!
I didn't do everything for you, but here is a basic outline.
You must finish it off.
int answer = 0;
int n;
for(n=1; n <= y; ++n)
{
answer = answer + (n! * 2^n)*y;
}
You're looking for the sum of (n! * 2^n) from 1 to y.
total variable is 0
for 1 until y:
multiply factorial(n) and pow(2, n) and add this to total
end of loop
print out total
First thing to do is to write down what you manually would do to work that equation out. Try out 1 to 5, for example, and see what you get.