26-08-2004, 08:09 PM
|
#1 (permalink)
|
|
Broken In
Join Date: Aug 2004
Posts: 101
|
can you tell me what mistake i made?
hi, i was writing a c program for my class assignment, and i got stuck. i paste it below.
Quote:
/*This program calculates the roots of the equation x^3-3x^2+5x-10=0
by two separate methods, Bisection method and Newton-Rhapson method*/
#include <stdio.h>
#include <math.h>
void bisect(void);
void newton(void);
main()
{
int choice;
printf("Choose the method you want to use\n");
printf("1:Bisection method\n");
printf("2:Newton-Rhapson\n");
scanf("%d", &choice);
/*switch(choice) {
case 1:*/
bisect();
/*case 2:
newton();*/
}
void bisect()
{
float x1,x2,x0,f1,f2,f0;
int count;
printf("Enter the range within which the calculation is to be carried out : ");
scanf("%f %f", &x1, &x2);
for(count=1;count<=50;count++)
{
f1=x1*x1-4*x1-10;
f2=x2*x2-4*x2-10;
if(f1*f2>0)
{
x0=(x1+x2)/2.0;
f0=x0*x0-4*x0-10;
printf("Iteration %d : x = %f \n", count, x0);
if(f1*f0<0)
{
x2=x0;
}
else
{
x1=x0;
f1=f0;
}
if(fabs((x2-x1)/x2)<.0005)
{
printf("\nThe required root of the equation is %f", x1);
break;
}
}
}
}
|
it appears to me that the program structure is ok, but when i run it, it does'nt give any output. i have also tried by taking the last 'printf' statement out of the for loop, but then the out put is not what i expected. it prints the value of x as -10 continously 50 times.  .
help plz!!
|
|
|
|
Advertisements. Register and be a member of the community to get rid of them.
|
|
Advertisement
|
|
26-08-2004, 08:11 PM
|
#2 (permalink)
|
|
Broken In
Join Date: Aug 2004
Posts: 101
|
ohhh, sorry! i gave the heading wrong. the equatin is x^2-4*x-10=0.
|
|
|
26-08-2004, 08:55 PM
|
#3 (permalink)
|
|
Apprentice
Join Date: May 2004
Location: Cyber City
Posts: 60
|
This is wat i used
// Prog for Bisection method
#include<math.h>
void main()
{
float fun(float x);
void bis(float *x,float a , float b, int *itr);
int itr=0,maxit;
float x,a,b,aerr,x1;
clrscr();
printf("\n\n\nEnter Value of a ");
scanf("%f",&a);
printf("\nEnter Value of b ");
scanf("%f",&b);
printf("\nEnter Value of allowed error ");
scanf("%f",&aerr);
printf("\nEnter Maximum Iteration allowed ");
scanf("%d",&maxit);
bis(&x,a,b,&itr);
do
{
if(fun(a)*fun(x)<0)
{b=x;}
else
{a=x;}
bis(&x1,a,b,&itr);
if(fabs(x1-x)<aerr)
{
printf("\n After %d Iterations ,Root is %.4f ",itr,x1);
getch();
exit(0);
}
x=x1;
}
while(itr<maxit);
{
printf("\n Solution Does not converge after %d iteration",maxit);
}
getch();
exit(1);
}
float fun(float x)
{
return ((x*x*x)-(x)-11);
}
void bis(float *x,float a,float b,int *itr)
{
*x=(a+b)/2;
++(*itr);
printf("\n Iteration number %3d x = %.4f ",*itr,*x);
}
|
|
|
26-08-2004, 09:41 PM
|
#4 (permalink)
|
|
Right Off the Assembly Line
Join Date: Aug 2004
Location: Mumbai
Posts: 40
|
I had to program in Pascal for CONM.
Which university r u from?
|
|
|
27-08-2004, 09:36 AM
|
#5 (permalink)
|
|
Broken In
Join Date: Dec 2003
Location: kolkata
Posts: 123
|
i think ur program is okay. just change x1 to x0 in the final output statement, coz what u need is the value of x0, and x1 will takethat value only if a particular condition is satisfied (f1*f0>0). also the equation you chose appears to have distributed roots, so you will have to try a number of ranges before u get a proper answer.
|
|
|
28-08-2004, 10:45 PM
|
#6 (permalink)
|
|
Broken In
Join Date: Mar 2004
Location: Chennai
Posts: 188
|
dood....dont tell me ur in eee as in electrical and electronics engg....
|
|
|
13-09-2004, 09:47 PM
|
#7 (permalink)
|
|
Right Off the Assembly Line
Join Date: Jul 2004
Location: Bangalore
Posts: 10
|
Thats pretty clear explanation. Follow it up.
|
|
|
14-09-2004, 01:42 AM
|
#8 (permalink)
|
|
Wise Old Owl
Join Date: Feb 2004
Location: Palghar, Mumbai
Posts: 1,000
|
here r my code
1 Bisection Method
Code:
/*
* Find root of equation xýSin(x)-xCos(x)+5 = 0 using bisection method
* upto 6 decimal places
*
* GUESS 1 & 2
*/
#include "math.h"
double function1(double);
void main() {
int i;
float a=0,b=0,c,fa,fb,fc;
long n,nc=0;
start:
clrscr();
printf("\n -----------------------------------------------------------");
printf("\n -------------------- BISECTION METHOD --------------------");
printf("\n -------------- y = f(x) = xýSin(x)-xCos(x)+5 --------------");
printf("\n -----------------------------------------------------------");
printf("\nEnter your guesses\
\n\nFirst guess\t: ");
scanf("%f",&a);
printf("\nSecond guess\t: ");
scanf("%f",&b);
fa=function1(a);
fb=function1(b);
if(fa*fb > 0) {
printf("\n\nWrong Guesses");
printf("\na=%f, f(a)=%f",a,fa);
printf("\nb=%f, f(b)=%f",b,fb);
getch();
goto start;
}
printf("\n\nNo. of iterations : ");
scanf("%ld",&n);
while(1) {
c=(a+b)/2;
nc++;
if(nc > n) break;
fc=function1(c);
fa=function1(a);
fb=function1(b);
printf("\nn=%02ld a=%+f b=%+f c=%+f fa=%+f fb=%+f fc=%+f",nc,a,b,c,fa,fb,fc);
if(fa*fc <= 0)
b=c;
else
if(fb*fc <= 0)
a=c;
if(fabsl(a-b) < 1e-6) break;
}
printf("\n\nFinal Value of root is \t c =%+.10f,\n\t\t\tf(c)=%+.10f",c,fc);
printf("\nSolution converges to root after %ld iterations",nc);
}
double function1(double x) {
return(x*x*sin(x)-x*cos(x)+5);
}
2. NR Method
Code:
/*
* Find root of equation x^3-2x+5 = 0 by Newton Raphson method upto
* five decimal places.
*
* GUESS = -2.1
*/
#include "math.h"
float f(float);
float fd(float);
void main(void);
float f(float a) {
return(a*a*a-2*a+5);
}
float fd(float a) {
return(3*a*a-2);
}
void main(void) {
float a,nr,dr,p;
float const error=1e-5;
int n,s;
start:
clrscr();
printf("\n -----------------------------------------------------------");
printf("\n ------------------ NEWTON RAPHSON METHOD ------------------");
printf("\n ------------------- y = f(x) = x^3-2x+5 -------------------");
printf("\n -----------------------------------------------------------");
printf("\n Enter your guess :: ");
scanf("%f",&a);
nr=f(a);
dr=fd(a);
n=1;
p=a;
printf("\n Enter max iterations :: ");
scanf("%d",&s);
while(n<=s) {
if(dr==0) {
printf("\nERROR! Division by zero.");
getch();
goto start;
}
printf("\n n=%2d a=%+f fa=%+f f'a=%f",n,a,nr,dr);
a-=(nr/dr);
nr=f(a);dr=fd(a);n++;
if( fabs(a-p) < error ) break;
p=a;
printf(" a=>>%+f",a);
}
printf("\n Root = %f\n Iterations = %d",a,n-1);
getch();
}
__________________
i generally prefer quality over quantity
1 aadi + 1 aadi = 1 full ;)
|
|
|
14-09-2004, 01:49 AM
|
#9 (permalink)
|
|
Wise Old Owl
Join Date: Feb 2004
Location: Palghar, Mumbai
Posts: 1,000
|
i even wrote a code to show how the solution converges to root graphically using Turbo C. I don't have that code, i was in my college and i am sure that college hdd must had be formatted atleast once in last 4 years otherwise i would have given that code too.
__________________
i generally prefer quality over quantity
1 aadi + 1 aadi = 1 full ;)
|
|
|
27-09-2004, 09:55 PM
|
#10 (permalink)
|
|
Right Off the Assembly Line
Join Date: Aug 2004
Location: Mumbai
Posts: 40
|
Guys i want to program a small application.
Like i want to write a letter as an exe file.
When downloaded n opened it should popup a dos window n the
text should appear one character at a time with a very short delay.
Can anyone tell me how to in Basic cos currently i dont have any c compiler.
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|