Sponsored Content
Full Discussion: some math problems in C
Top Forums Programming some math problems in C Post 302132686 by cdfd123 on Sunday 19th of August 2007 12:30:18 AM
Old 08-19-2007
some math problems in C

I want to calculate secant method using C language
That is a program---->
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
main()
{
double fx(double x);
double x0,x1,x2,f0,f1,f2,err;
int n,i;
printf("\n\n f(x) =x*x*x-5*x-7");
printf("\n\nEnter an interval [x0,x1] in"
" which root is to be found");
printf("\nx0 =");
scanf("%f",&x0); /* INTERVAL[x0,x1] is to be entered here */
printf("x1=");
scanf("%f",&x1);
printf("\n Enter the number of iterations=");
scanf("%d",&n);
printf("\npress any key for display of iterations...\n");
getchar();
i=0;
while (n > 0)
{
f0=fx(x0);
f1=fx(x1);
x2 = x1-((x1-x0)/(f1-f0))*f1;
i++;
printf("\n x[%d]=%f x[%d]=%f",i,i-1,x0,i,x1);
printf("\n f[%d]=%f f[%d]=%f",i,i-1,f0,i,f1);
printf("\n x[%d]=%f",i+1,x2);
x0=x1;
x1=x2;
getchar();
}
printf("\n\nThe value of root is =%f",x2);
}
double fx(double x)
{
double f;
f=x*x*x-5*x-7;
return(f);
}

But while running not the erreo is displayed but the results displayed is
f(x) =x*x*x-5*x-7

Enter an interval [x0,x1] in which root is to be found
x0 =2.5
x1=3

Enter the number of iterations=4

press any key for display of iterations...

x[1]=0.000000 x[0]=0.000000
f[1]=-7.000000 f[0]=-7.000000
x[2]=inf

Means not able to calculate root as seen in the output it is x[1]=0...

so what will be the problem????
 

8 More Discussions You Might Find Interesting

1. Programming

math.h not working? o.0

Alright, umm i cant get this to work. im looking at some example and a book i have. when i try to compile my program i get an error message. ld: 0711-317 ERROR: Undefined symbol: .sqrt ld: 0711-345 Use the -bloadmap or -bnoquiet option to obtain more information. I did #include<math.h> after my... (2 Replies)
Discussion started by: primal
2 Replies

2. Programming

something about <math.h>

Hi, I got an easy problem for you but really difficult for me 'cause I am pretty new to this field I got header file <math.h> included in my .c file , then I write the code as below: k = sqrt(i); /* both variables k and i are int */ then I cc temp.c it says like this undefined... (4 Replies)
Discussion started by: blf0
4 Replies

3. UNIX for Dummies Questions & Answers

math in unix

I have 2 variables a=2 b=1 i want to add a and b how do i do this in unix using just the echo command and by assigning it to a different variable like c? (13 Replies)
Discussion started by: khestoi
13 Replies

4. Shell Programming and Scripting

math help

$ x=1 $ y=1.5 $ z=$((x*y)) bash: 1.5: syntax error: invalid arithmetic operator (error token is ".5") What's wrong? (2 Replies)
Discussion started by: rockbike
2 Replies

5. Shell Programming and Scripting

Need help with AWK math

I am trying to do some math, so that I can compare the average of six numbers to a variable. Here is what it looks like (note that when I divide really big numbers, it isn't a real number): $ tail -n 6 named.stats | awk -F\, '{print$1}' 1141804 1140566 1139429 1134210 1084682 895045... (3 Replies)
Discussion started by: brianjb
3 Replies

6. Programming

Math function with C

I have int miles, yards; float kilometers; float kilometers2; miles = 26; yards = 385; kilometers = 1.609 * (miles + yards / 1760.0); where int/float remains a float. How ever if I change it to kilometers = 1.609 * (miles + yards / 1760); ... (7 Replies)
Discussion started by: Fingerz
7 Replies

7. UNIX for Dummies Questions & Answers

Linux Math Help

I am struggling with scripting this challenge a friend and I have. You have file1 and its contents is a single number you have file 2 and its contents are a different number you want to add file1 to file2 and have the output be put into file3 (3 Replies)
Discussion started by: minkyboodle
3 Replies

8. UNIX for Dummies Questions & Answers

Math

i have file (my_file.txt) that looks like this: 000000000000010000 000000000000010000 000000000000005000 000000000000005000 000000000000005000 000000000000005000 000000000000005000 000000000000005000 000000000000005000 000000000000005000 all said and one, it should look... (11 Replies)
Discussion started by: lawsongeek
11 Replies
CATAN(3)						     Linux Programmer's Manual							  CATAN(3)

NAME
catan, catanf, catanl - complex arc tangents SYNOPSIS
#include <complex.h> double complex catan(double complex z); float complex catanf(float complex z); long double complex catanl(long double complex z); Link with -lm. DESCRIPTION
These functions calculate the complex arc tangent of z. If y = catan(z), then z = ctan(y). The real part of y is chosen in the interval [-pi/2,pi/2]. One has: catan(z) = (clog(1 + i * z) - clog(1 - i * z)) / (2 * i) VERSIONS
These functions first appeared in glibc in version 2.1. ATTRIBUTES
For an explanation of the terms used in this section, see attributes(7). +----------------------------+---------------+---------+ |Interface | Attribute | Value | +----------------------------+---------------+---------+ |catan(), catanf(), catanl() | Thread safety | MT-Safe | +----------------------------+---------------+---------+ CONFORMING TO
C99, POSIX.1-2001, POSIX.1-2008. EXAMPLE
/* Link with "-lm" */ #include <complex.h> #include <stdlib.h> #include <unistd.h> #include <stdio.h> int main(int argc, char *argv[]) { double complex z, c, f; double complex i = I; if (argc != 3) { fprintf(stderr, "Usage: %s <real> <imag> ", argv[0]); exit(EXIT_FAILURE); } z = atof(argv[1]) + atof(argv[2]) * I; c = catan(z); printf("catan() = %6.3f %6.3f*i ", creal(c), cimag(c)); f = (clog(1 + i * z) - clog(1 - i * z)) / (2 * i); printf("formula = %6.3f %6.3f*i ", creal(f2), cimag(f2)); exit(EXIT_SUCCESS); } SEE ALSO
ccos(3), clog(3), ctan(3), complex(7) COLOPHON
This page is part of release 4.15 of the Linux man-pages project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at https://www.kernel.org/doc/man-pages/. 2015-04-19 CATAN(3)
All times are GMT -4. The time now is 03:09 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy