#include <stdio.h>
#include <conio.h>
#include <math.h>
#define ESP 0.0001
#define F(x) x^3-2.5x^2-1.8x+2.356
void main()
{
float x0,x1,x2,f1,f2,f0;
int count=0;
do
{
printf("\nEnter the value of x0: ");
scanf("%f",&x0);
}while(F(x0) > 0);
do
{
printf("\nEnter the value of x1: ");
scanf("%f",&x1);
}while(F(x1) < 0);
printf("\n__________________________________________________________\n");
printf("\n x0\t x1\t x2\t f0\t f1\t f2");
printf("\n__________________________________________________________\n");
do
{
f0=F(x0);
f1=F(x1);
x2=x0-((f0*(x1-x0))/(f1-f0));
f2=F(x2);
printf("\n%f %f %f %f %f %f",x0,x1,x2,f0,f1,f2);
if(f0*f2<0)
{
x1=x2;
}
else
{
x0 = x2;
}
}while(fabs(f2)>ESP);
printf("\n__________________________________________________________\n");
printf("\n\nApp.root = %f",x2);
getch();
}
The program can't seem to read the #define F(x) x^3-2.5x^2-1.8x+2.356 as function but when I use #define F(x) 3*(x) - 1 - cos(x) there's no error.
I tried #define F(x) (x)^3-2.5(x)^2-1.8(x)+2.356, no luck.
F(x) x^3-2.5x^2-1.8x+2.356
^ is not the power operator in C use pow(), powf() function (but in this case there is no need of it)
2.5x has to be 2.5 * x - buy a book about C
do not #define functions make them "real"functions
Do not use double literals in float expressions
float F(float x)
{
return x * x * x + 2.5f * x * x + 1.8f * x + 2.356f;
}
Related
The teacher asks to remove the pi subtraction cycle in the main function. I don’t know how to write the program so that the correct results will come out for any values.
#include <stdio.h>
#include <math.h>
double sinus(double x);
int main(void) {
double a, x;
scanf("%le", & x);
a = x;
while (fabs(x) > 2 * (M_PI)) {
x = fabs(x) - 2 * (M_PI);
}
if (a > 0)
a = sinus(x);
else a = (-1) * sinus(x);
printf("%le", (double) a);
return 0;
}
double sinus(double x) {
double sum = 0, h, eps = 1.e-16;
int i = 2;
h = x;
do {
sum += h;
h *= -((x * x) / (i * (i + 1)));
i += 2;
}
while (fabs(h) > eps);
return sum;
return 0;
}
#include <stdio.h>
#include <math.h>
double sinus(double x);
int main(void)
{
double a,x;
scanf("%le",&x);
a=x;
x=fmod(fabs(x),2*(M_PI));
if(a>0)
a=sinus(x);
else a=(-1)*sinus(x);
printf("%le",(double)a);
return 0;}
double sinus(double x)
{
double sum=0, h, eps=1.e-16; int i=2;
h=x;
do{
sum+=h;
h*=-((x*x)/(i*(i+1)));
i+=2;}
while( fabs(h)>eps );
return sum;
return 0;
}
… how to write the program so that the correct results will come out for any values.
OP's loop is slow with large x and an infinfite loop with very large x:
while (fabs(x) > 2 * (M_PI)) {
x = fabs(x) - 2 * (M_PI);
}
A simple, though not high quality solution, is to use fmod() in the function itself. #Damien:
#ifndef M_PI
#define M_PI 3.1415926535897932384626433832795
#endif
double sinus(double x) {
x = fmod(x, 2*M_PI); // Reduce to [-2*M_PI ... 2*M_PI]
...
Although function fmod() is not expected to inject any error, the problem is that M_PI (a rational number) is an approximation of π, (an irrational number). Using that value approximation injects error especially x near multiplies of π. This is likely OK for modest quality code.
Good range reduction is a problem as challenging as the trigonometric functions themselves.
See K.C. Ng's "ARGUMENT REDUCTION FOR HUGE ARGUMENTS: Good to the Last Bit" .
OP's sinus() should use additional range reduction and trigonometric properties to get x in range [-M_PI/4 ... M_PI/4] (example) before attempting the power series solution. Otherwise, convergence is slow and errors accumulate.
-use double precision
-use sqrt() and exponential function exp()
-use * to compute the square
-do not use pow()
I am getting values they are just not anything as to what I expected. I tried making them all signed but it didn't change anything and I've tried printing out with 12 decimal places and nothing seems to be working.I have linked the math library and defined it as well.
double normal(double x, double sigma, double mu)
{
double func = 1.0/(sigma * sqrt(2.0*M_PI));
double raise = 1.0/2.0*((x-mu)/sigma);
double func1 = func * exp(raise);
double comp_func = (func1 * func1);
return comp_func;
}
int main(void)
{
// create two constant variables for μ and σ
const double sigma, mu;
//create a variable for x - only dynamic variable in equation
unsigned int x;
//create a variable for N values of x to use for loop
int no_x;
//scaniing value into mu
printf("Enter mean u: ");
scanf("%lf", &mu);
//scanning value into sigma
printf("Enter standard deviation: ");
scanf("%lf", &sigma);
//if sigma = 0 then exit
if(sigma == 0)
{
printf("error you entered: 0");
exit(0);
}
//storing number of x values in no_x
printf("Number of x values: ");
scanf("%d", &no_x);
//the for loop where i am calling function normal N times
for(int i = 1; i <= no_x; i++)
{
//printing i for the counter in prompted x values
printf("x value %d : ", i);
// scanning in x
scanf("%lf", &x);
x = normal(x,sigma,mu);
printf("f(x) = : %lf.12", x);
printf("\n");
}
return 0;
}
C:>.\a.exe
Enter mean u: 3.489
Enter std dev s: 1.203
Number of x values: 3
x value 1: 3.4
f(X) = 0.330716549275
x value 2: -3.4
f(X) = 0.000000025104
x value 3: 4
f(X) = 0.303015189801
But this is what I am receiving
C:\Csource>a.exe
Enter mean u: 3.489
Enter standard deviation: 1.203
Number of x values: 3
x value 1 : 3.4
f(x) = : 15086080.000000
x value 2 : -3.4
f(x) = : 15086080.000000
x value 3 : 4
f(x) = : 1610612736.000000
Insert these lines:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
Change:
const double sigma, mu;
to:
double sigma, mu;
Change:
unsigned int x;
to:
double x;
Replace the definition of the normal function with:
double normal(double x, double sigma, double mu)
{
double func = 1.0/(sigma * sqrt(2.0*M_PI));
double t = (x-mu)/sigma;
return func * exp(-t*t/2);
}
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#ifndef M_PI
#define M_PI (3.14159265358979323846)
#endif
#include<math.h>
#include<stdio.h>
#include <stdlib.h>
double normal(double x, double sigma, double mu)
{
double func = 1.0/(sigma * sqrt(2.0*M_PI));
double t = (x-mu)/sigma;
return func * exp((-0.5*t)* t);
}
I Finally got this code above working after tweaking with it literally all day lol, C math can be rather tricky, thank you for the help above as well.
Say I have a high floating point number... 1345.23
I want to reduce it by 2*PI until it stays between -PI and +PI so I'd do:
#define PI 3.14159265358f
#define TWO_PI 6.28318530718f
float a = 1345.23f;
while (a > PI) a -= TWO_PI;
Do you know a fastest method?
With this code you will enter in the loop just 1 time (you can delate it adding just a more a -= TWO_PI
#include <stdio.h>
#define PI 3.14159265358f
#define TWO_PI 6.28318530718f
int main(void) {
float a = 1345.23f;
float b = 1345.23 - PI;
int c = b/TWO_PI;
a -= c*TWO_PI;
int i = 0;
while (a > PI){
a -= TWO_PI;
printf("%d",i++);
}
printf("\na : %f",a);
}
OUTPUT:
0
a : 0.628314
While your code will do the cicle :
214 times
BETTER CODE:
#include <stdio.h>
#define PI 3.14159265358f
#define TWO_PI 6.28318530718f
#define INV_TWO_PI 0.15915494309189533
int main(void) {
double a = 1345.23;
if(a > PI){
double b = a - PI; // you get the distance between a and PI
// int c = b/TWO_PI; // you get the integer part
int c = b * INV_TWO_PI; // the same as above using multiplication
a -= (c+1)*TWO_PI; // you just subtract c+1 times TWO_PI
// c+1 cause you want come in the range [-PI,PI]
}
}
Not the fastest, but the shortest code:
y = asin(sin(a));
Assuming that your code has to do with phase wrapping in radians, so that values between PI and TWO_PI can map between -PI and 0.0 a simple and fast solution would be:
double a = 1345.23;
double b = TWO_PI;
double c = (fmod(a,b) > PI ? fmod(a,b) - b : fmod(a,b));
After accept answer
To quickly reduce between -PI and PI, simply use remquof();
#include <math.h>
#include <stdio.h>
float reduce_radian(float x) {
static const float pi2 = 6.283185307179586476925286766559f;
int n;
return remquof(x, pi2, &n);
}
int main(void) {
printf("x % .10e\ty % .10e\n", 1e-30, reduce_radian(1e-30));
for (float x = 0.0f; x <= 4.0f; x += 1.0f) {
printf("x % .10f\ty % .10f\n", -x, reduce_radian(-x));
printf("x % .10f\ty % .10f\n", x, reduce_radian(x));
}
}
Output
x 1.0000000000e-30 y 1.0000000032e-30
x -0.0000000000 y -0.0000000000
x 0.0000000000 y 0.0000000000
x -1.0000000000 y -1.0000000000
x 1.0000000000 y 1.0000000000
x -2.0000000000 y -2.0000000000
x 2.0000000000 y 2.0000000000
x -3.0000000000 y -3.0000000000
x 3.0000000000 y 3.0000000000
x -4.0000000000 y 2.2831854820
x 4.0000000000 y -2.2831854820
To understand why this is not the best precise answer, is a deep subject.
See K.C. Ng's "ARGUMENT REDUCTION FOR HUGE ARGUMENTS: Good to the Last Bit"
I have the following code in C to make some arithmatic calculation
#include <stdio.h>
#include <stdlib.h>
int main()
{
float x,y;
float z;
printf("Enter x y z \n");
scanf("%f %f %f ", &x, &y , &z);
z = ((4.2 (x+y)))/ (z - (0.25*z))/ (y+z)/ ((x+y) * (x+y));
printf("\n z = %f", z);
return 0;
}
when i build the program , i get the following error message in the following code line
z = ((4.2 (x+y)))/ (z - (0.25*z))/ (y+z)/ ((x+y) * (x+y));
called object is not a function or dunction pointer
That's a typo, you're missing an operator:
z = ((4.2 * (x + y))) / (z - (0.25*z)) / (y + z) / ((x + y) * (x + y));
^
whatever the operator is
C has no support for mathematics-implicit multiplication operator (more or less as you would write in an equation in school). E.g.
// y = 2x
int y;
y = 2 * x;
So I have to make this formula "y = y / (3/17) - z + x / (a % 2) + PI" in C
I am having a problem with (a%2) as it is returning odd values. ie 1%2 = 0.000001
int assignment7()
{
#define PI 3.14
int a=0,amod2;
double Ny=0,y=0,z=0,x=0;
printf("Enter values for x,y,z and a: ");
scanf("%d%lf%lf%lf",&a,&y,&z,&x);
//printf("%d,%lf,%lf,%lf\n",a,y,z,x);
//amod2=1%2;
//printf("%lf",amod2);
Ny=y/(double)(3/17) - z+x / amod2 + PI;
printf("%lf\n",Ny);
When you say:
printf("%lf",amod2);
the compiler expects amod2 to be a "long float" (aka a double), but you defined it as:
int amod2;
Also your prompt says "x,y,z and a" but you read in the order "a,y,z,x":
printf("Enter values for x,y,z and a: ");
scanf("%d%lf%lf%lf",&a,&y,&z,&x);
that's awkward at best.
EDIT: cleaned up a bit and made some assumptions about order of operations:
#include <stdio.h>
#define PI 3.14
#define DIVSOR (3.0/17.0)
int assignment7 ( void );
int assignment7 ( void ) {
double x = 0.0;
double y = 0.0;
double z = 0.0;
int a = 0;
int amod2;
double Ny;
printf("Enter values for x,y,z and a: ");
scanf("%lf%lf%lf%d",&x,&y,&z,&a);
amod2 = a % 2;
Ny = (y / DIVSOR) - z + (x / amod2) + PI;
printf("%lf\n", Ny);
return 0;
}
int main ( void ) { return assignment7(); }
You don't say what inputs you are giving it, (a test case with inputs and the expected results would be super helpful), but I can point out that x / (a % 2) is going to be infinity when a is 2 or 4 or 6 or ...