Home › Forums › C Programming › some math problems in C

- This topic has 0 replies, 1 voice, and was last updated 13 years, 6 months ago by EvangelineBush33.

Viewing 0 reply threads

- AuthorPosts
- August 18, 2007 at 5:57 pm #1997EvangelineBush33Participant
I want to calculate secant method using C language

That is a program—->1234567891011121314151617181920212223242526272829303132333435363738394041#include<stdio.h><br />#include<math.h><br />#include<stdlib.h><br />main()<br />{<br />double fx(double x);<br />double x0,x1,x2,f0,f1,f2,err;<br />int n,i;<br />printf("nn f(x) =x*x*x-5*x-7");<br />printf("nnEnter an interval [x0,x1] in"<br />" which root is to be found");<br />printf("nx0 =");<br />scanf("%f",&x0); /* INTERVAL[x0,x1] is to be entered here */<br />printf("x1=");<br />scanf("%f",&x1);<br />printf("n Enter the number of iterations=");<br />scanf("%d",&n);<br />printf("npress any key for display of iterations...n");<br />getchar();<br />i=0;<br />while (n > 0)<br />{<br />f0=fx(x0);<br />f1=fx(x1);<br />x2 = x1-((x1-x0)/(f1-f0))*f1;<br />i++;<br />printf("n x[%d]=%f x[%d]=%f",i,i-1,x0,i,x1);<br />printf("n f[%d]=%f f[%d]=%f",i,i-1,f0,i,f1);<br />printf("n x[%d]=%f",i+1,x2);<br />x0=x1;<br />x1=x2;<br />getchar();<br />}<br />printf("nnThe value of root is =%f",x2);<br />}<br />double fx(double x)<br />{<br />double f;<br />f=x*x*x-5*x-7;<br />return(f);<br />}But while running not the erreo is displayed but the results displayed is

f(x) =x*x*x-5*x-7Enter an interval [x0,x1] in which root is to be found

x0 =2.5

x1=3Enter 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]=infMeans not able to calculate root as seen in the output it is x[1]=0…

so what will be the problem????

- AuthorPosts

Viewing 0 reply threads

- The forum ‘C Programming’ is closed to new topics and replies.