I'm trying to calculate sin and cos without using math library with taylor series witch is
sinx =∑n=0 to n=∞ (-1)^n * x^(2n+1) / (2n+1)! and this code produces whatever the input is 0,00000 for sin 1,00000 for cos where is the problem in this code
#include <stdio.h>
#include <stdlib.h>
double fakt(int);
double power(int,int);
int negative_positive(int);
double calculate_sin(int,int);
double calculate_cos(int,int);
double calculate_radyan(int);
int main() {
int degree,number;
char command;
do{
scanf("%c",&command);
if(command=='d' || command=='D'){
scanf("%d %d",°ree,&number);
double radyan = calculate_radyan(degree);
printf("%lf \n%lf ",calculate_sin(radyan,number),calculate_cos(radyan,number));
}
}while(command!='e' && command!='E');
return 0;
}
double fakt(int n){
int i;
double result=1;
for(i=1;i<n;i++)
result = result*i;
return result;
}
double power(int base, int exponent){
int i;
double result=1;
for(i=0;i<exponent;i++)
result = result*base;
return result;
}
int negative_positive(int number){
if(number % 2 == 0)
return 1;
else
return -1;
}
double calculate_sin(int degree , int n){
int i;
double result=0;
double tmp=0;
for(i=0;i<n;i++){
tmp=negative_positive(i)*power(degree,2*i+1)/fakt(2*i+1);
result=tmp+result;}
return result;
}
double calculate_cos(int degree , int n){
int i;
double result=0;
double tmp=0;
for(i=0;i<n;i++)
{
tmp=negative_positive(i)*power(degree,i*2)/fakt(2*i);
result=tmp+result;
}
return result;
}
double calculate_radyan(int degree){
double result,pi=3.14159;
result =pi/180*degree;
return result;
}
ı will answer my own question but ı found the solution. when program trys to go to power method with double radyan variable it converts that variable to 0 cause that method just takes integer variables power(double base,int exponent) is solved my problem
Related
I have a comment from my teacher related to my code for bisection method. He said "The number of calculations of the function is not optimised. In fact, on each iteration, the function is calculated three times, although it would be enough once."
Could you please help me to optimise the calculations. I actually don't even see at which point the function is calculated 3 times.
#include<stdio.h>
#include<math.h>
#include<time.h>
double f(double x); //Function
int res(int i, double a, double b, double ξ, double ε1, double ε2); //Print result
double Bisection(double a, double b, double ε1, double ε2); //Bisection method
int main()
{
double a=-10, b=0, ξ, h=0.5, α, x1, x2, ε1, ε2;
int i=0;
printf("\nf(x) = 2^x - 2 * cos(x)");
printf("\nStart of the interval a = %.0lf", a);
printf("\nEnd of the interval b = %.0lf", b);
printf("\nEnter error ε1 for function = ");
scanf("%lf", &ε1);
printf("Enter error ε2 for argument = ");
scanf("%lf", &ε2);
printf("\n\nSOLUTION:");
//selection of roots
x1=a;
x2=x1+h;
while (x2<=b)
{
if ((f(x1)*f(x2))<0)
{
i++;
printf("\n\n%d) %d root of the function is in the interval [%.1f, %.1f]\n",i, i, x1, x2);
printf("\nn\t a\t\t b\t\t ξ\t f(ξ)\t ε1\t\t ε2\n");
Bisection(x1,x2,ε1,ε2); //Bisection method
}
x1=x2;
x2=x1+h;
}
return 0;
}
//Function
double f(double x)
{
double y;
y=pow(2,x)-2*cos(x);
return y;
}
//Print result
int res(int i, double a, double b, double ξ, double ε1, double ε2)
{
printf("%d\t%10.7f %10.7f %10.7f %10.7f %e %e\n", i, a, b, ξ, f(ξ), ε1, ε2);
return 0;
}
//Bisection method
double Bisection(double a, double b, double ε1, double ε2)
{
double ξ=(a+b)/2; //Middle of the interval
double α;
int i=0;
if (f(ξ)==0)
{
printf("Root: %f \n\n", ξ);
}
else
{
while ((fabs(f(ξ))>ε1) && ((fabs(b-a)/2)>ε2)) //The accuracy of the definition of the root
{
if ((f(a)*f(ξ))<0)
{
b=ξ;
}
else
{
a=ξ;
}
ξ=(a+b)/2;
res(i+1, a, b, ξ, ε1, ε2); //Print results
i++;
}
printf("Root ξ=%.7f found after %d iterations\n", ξ, i);
printf("Function f(ξ)=%.10f found after %d iterations\n", f(ξ), i);
}
return 0;
}
Results
Only including relevant lines
Replace:
double Bisection(double a, double b, double ε1, double ε2)
{
double ξ=(a+b)/2;
if (f(ξ)==0)
else
{
while ((fabs(f(ξ))>ε1) && ((fabs(b-a)/2)>ε2))
{
if ((f(a)*f(ξ))<0)
ξ=(a+b)/2;
}
}
with
double Bisection(double a, double b, double ε1, double ε2)
{
double ξ=(a+b)/2;
double val = f(ξ); // Here is the magic
if (val==0)
else
{
while ((fabs(val)>ε1) && ((fabs(b-a)/2)>ε2))
{
if ((f(a) * val )<0)
ξ=(a+b)/2;
val = f(ξ); // And here
}
}
It's not really some secret trick we're talking about. It's only about saving the return value in a variable and use that variable instead of a function call whenever possible. If the argument to the function changes, then you need to execute the function again. Here is a simple example that makes a string uppercase.
void uppercase(char *str) {
// Bad, because it's not necessary to calculate the string
// length every iteration.
for(size_t i=0; i<strlen(str); i++)
str[i] = toupper(str[i]);
}
// Instead, do this
void uppercase(char *str) {
size_t len = strlen(str); // Precalculate and reuse in loop
for(size_t i=0; i<len; i++)
str[i] = toupper(str[i]);
}
long double power1(double x, int n) {
long double power = 1;
int i;
for(i=1; i<=n; i++) {
power *= i;
}
return power;
}
long long int factorial(long long n) {
long long int pr = 1;
long long int i;
for(i=1; i<=n; i++){
pr *= i;
}
return pr;
}
double myExp(double x, double epsi) {
double sum=(double)1;
int i;
while(power1(x, i)/((1.0)*factorial(i)) - epsi <= 0) {
sum += power1(x, i)/(1.0*factorial(i));
i++;
}
return sum;
}
int main(int argc, char *argv[]) {
system("cls");
double x, epsi;
int n;
x=1.5;
epsi=0.00001;
n=1000;
printf("exp(%.lf,%f)=%f\n", x, epsi, myExp(x, epsi));
printf("\n");
}
I need help to fix my exponential function e^x given the epsi value and x value such that abs(x^n/n!) <= epsi, where n is the first integer satisfying the condition. It seems the while loop doesn't work. When I enter 1.5, the desired output is 4.481689, but the result is 1.0000.
I have to use:
float average(const int tab[], int size);
float stdDev(const int tab[], int size);
to printf average and stdDev in C.
I have problem with average and i think with const int.
When i add const int tab[101] i have error with a1;
So how can i make it work with const int (if i can).
And if it is anything wrong with this code.
Any help will be helpful.
#include<stdio.h>
#include<math.h>
float average(const int tab[], int size);
float stdDev(const int tab[], int size);
int main()
{
float ave, std;
int a1;
int j;
int tab[101];
printf("Podaj liczby: ");
for(j=0; j<=99; j++)
{
a1 = scanf("%d", &tab[j]);
if(a1<1)
{
printf("Incorrect input");
return 1;
}
if(tab[0]==0)
{
printf("not enough data available");
return 2;
}
if(tab[j]==0)
{
break;
}
}
ave = average(tab, j);
printf("%.2f\n", ave);
std = stdDev(tab, j);
printf("%.2f", std);
return 0;
}
float average(const int tab[], int size)
{
int i;
float y=0, x;
if(size<=0)
{
return -1;
}
for(i=0; i<size; i++)
{
x = x + tab[i];
}
y = x/size;
return y;
}
float stdDev(const int tab[], int size)
{
int i;
float y, z, z1, z2=0, z3=0;
if(size<=0)
{
return -1;
}
y = average(tab, size);
for(i=0; i<size; i++)
{
z = tab[i] - y;
z1 = pow(z, 2);
z2 = z2 + z1;
z=0;
z1=0;
}
z3 = sqrt(z2/size);
return z3;
}
You define the variable x in average here:
float y=0, x;
without giving it a value. Then here:
x = x + tab[i];
you are reading its value without setting it anywhere beforehand. Because you never gave x a value, its value will be indeterminate and reading it will cause undefined behavior, which means that your program could e.g. print garbage output.
Always initialize your variables:
float y=0, x=0;
I wish to write a program which calculates the series x-(x^3/3!)+(x^5/5!)-(x^7/7!)+...(x^n/n!) by taking x and n as user inputs.
This is what i've tried, and well there's no output when I enter the values for x,n:
#include<stdio.h>
#include<math.h>
//#include<process.h>
#include<stdlib.h>
double series(int,int);
double factorial(int);
int main()
{
double x,n,res;
printf("This program will evaluate the following series:\nx-(x^3/3!)+(x^5/5!)-(x^7/7!)+...(x^n/n!)\n");
printf("\nPlease enter a value for x and an odd value for n\n");
scanf("%lf%lf",&x,&n);
/*if(n%2!=0)
{
printf("Please enter a positive value!\n");
exit(0);
}*/
res=series(x,n);
printf("For the values you've entered, the value of the series is:\n %lf",res);
}
double series(int s, int t)
{
int i,sign=1; double r,fact,exec;
for(i=1;i<=t;i+2)
{
exec=sign*(pow(s,i)/factorial(i));
r+=exec;
sign*=-1;
}
return r;
}
double factorial(int p)
{
double f=1.0;
while(p>0)
{
f*=p;
p--;
}
return f;
}
When I enter values for x and n, it simply shows nothing.
While I've written in C, C++ solutions are also appreciated.
Output window in code::blocks
The loop
for(i=1;i<=t;i+2)
in the function series() is an infinite loop when t >= 1 because i isn't updated in the loop. Try changing + to += and use
for(i=1;i<=t;i+=2)
instead. Also it seems you should use type int for x and n in the function main() because the arguments of series() is int. Don't forget to change the format specifier when changing their types.
Thanks to all those who helped. Here's the final working code:
#include<stdio.h>
#include<math.h>
#include<process.h>
#include<stdlib.h>
double series(int,int);
double factorial(int);
int main()
{
int x,n; double res;
printf("This program will evaluate the following series:\nx-(x^3/3!)+(x^5/5!)-(x^7/7!)+...(x^n/n!)\n");
printf("\nPlease enter a value for x and an odd value for n\n");
scanf("%d%d",&x,&n);
if(n%2==0)
{
n=n-1;
}
res=series(x,n);
printf("For the values you've entered, the value of the series is:\n%lf",res);
}
double series(int s, int t)
{
int i,sign=1; double r=0.0,fact,exec;
for(i=1;i<=t;i+=2)
{
exec=sign*(pow(s,i)/factorial(i));
r+=exec;
sign*=-1;
}
return r;
}
double factorial(int p)
{
double f=1;
while(p>0)
{
f*=p;
p--;
}
return f;
}
in loop we step by two for getting odd numbers.by multiplying the current temp variable by the previous temp variable in the loop with neccesary terms like x square and dividing by i*(i-1) i.e for factorial and multiply with -1 i.e to achive negavtive number alternatively. by using this temp variable and adding it to sum variable in every iteration will give us answer.
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int n, x;
cout << "enter x and no.of terms: ";
cin >> x >> n;
float sum = 0, temp = x;
for (int i = 3; i < 2 * n + 2; i = i + 2)
{
temp = ((-1 * temp) *(x*x)) / i*(i-1);
sum = sum + temp;
}
cout << x + sum;
return 0;
}
// series x-(x^3/3!)+(x^5/5!)-(x^7/7!)+...(x^n/n!)
#include<stdio.h>
#include<math.h>
double factorial (int);
double calc (float, float);
int
main ()
{
int x, deg;
double fin;
printf ("x-(x^3/3!)+(x^5/5!)-(x^7/7!)+...(x^n/n!)\n");
printf ("Enter value of x\n");
scanf ("%d", &x);
printf ("highest degree in denom i.e., 1 or 3 or 5 whatever, it should be odd .\n");
scanf ("%d", °);
fin = calc (x, deg);
printf ("the summation of series =%1f\n", fin);
return 0;
}
double calc (float num, float res)
{
int count, sign = 1;
double rres = 0;
for (count = 1; count <= res; count += 2)
{
rres += sign * (pow (num, count) / factorial (count));
sign *= -1;
}
return (rres);
}
double factorial (int num)
{
int count;
double sum = 1;
for (count = 1; count <= num; count++)
{
sum *= count;
}
return (sum);
}
Hi I am newer to c language and When I compile the current code to find the variance I get the following error: expected expression before']' token var=var+pow((x[]-my_mean(n,double x[])),2);
#include
#include
double my_var(int n, double x[]);
double my_mean(int n, double x[]);
double my_sum(int n, double x[]);
int main (void)
{
int n=5;
double x[]={4, 6, 2, 7, 9};
my_var(n,x);
return 0;
}
double my_mean( int n , double x[])
{
return my_sum(n, x)/ n;
}
double my_sum(int n , double x[])
{
int i;
double s=0;
for( i= 0; i<n ;i++)
{
s = s + x[i];
}
return s;
}
double my_var(int n, double x[])
{
double var;
int i;
for (i=0;i<n;i++)
{
var=var+pow((x[]-my_mean(int n,double x[])),2);
}
return var;
}
Okk.. I have got something figured out with you code.. Have a look::
#include <stdio.h>
#include <math.h>
double my_var(int n, double x[]);
double my_mean(int n, double x[]);
double my_sum(int n, double x[]);
int main (void)
{
int n=5;
double x[]={4, 6, 2, 7, 9};
printf("%f",my_var(n,x));
return 0;
}
double my_mean( int n , double x[])
{
return my_sum(n, x)/ n;
}
double my_sum(int n , double x[])
{
int i;
double s=0;
for( i= 0; i<n ;i++)
{
s = s + x[i];
}
return s;
}
double my_var(int n, double x[])
{
double var=0;
int i;
for (i=0;i<n;i++)
{
var=var+pow((x[i]-my_mean(n,x)),2);
}
return var;
}
Just added a print statement at the end of the main to check the result. In the for loop of your code in the function double my_var(int n, double x[]) you have a statement ::var=var+pow((x[]-my_mean(int n,double x[])),2);, firstly you cannot specify the data types in the function calls. Further, in the pow function you write (x[]-my_mean(int n,double x[])),2, instead of x[] it should be x[i], you need to specify an index.
Hope this helps.
and when you include math.h in your program then you have to compile it by the command "gcc my_program.c -o my_program -lm"
You forgot to use the loop index. Also, remove types from your call.
var=var+pow((x[i]-my_mean(n,x[i])),2);