2 Replies - 6838 Views - Last Post: 15 September 2009 - 10:26 PM Rate Topic: ***** 1 Votes

#1 Sandthief  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 07-June 09

Bisection and False Position Methods

Posted 15 September 2009 - 09:07 PM

Well, first of all, I'll place the code, Bisec is Bisection Method and FalPos is False Position, both are made to find approximate roots of a given math function.

#include <stdlib.h>
#include <stdio.h>
#include <math.h>

double Raices(double xi, double xd, double errtolerado, int maxiter, int *ban, double (*metodo)(double xder, double xizq, double (*func)(double y)), double (*f)(double a, double (*curva)(double x)))
{
	   double xa, xv, erraprox;
	   if(f(xi,sin)*f(xd,sin)>=0)
	   {
						 *ban=0;
						 return 0;
	   }
	   
	   
	   for(int n=1; n<=maxiter; n++)
	   {
			   xa=metodo(xi, xd, f);
			   if(f(xi,sin)*f(xa,sin)<0)
			   xd=xa;
			   else
			   {
								if(f(xi,sin)*f(xa,sin)>0)
								xi=xa;
								else
								{
												 *ban=2;
												 return xa;
								}
			   }
			   if(n>1)
			   erraprox=fabs((xa-xv)/(xa));
			   xv=xa;
			   if(erraprox<=errtolerado)
			   {
										*ban=2;
										return xa;
			   }
	   }
	   *ban=1;
	   return xa;
}

double Bisec(double xi, double xd, double (*f)(double x))
{
	   double xa;
	   xa=(xi+xd)/2;
	   return xa;
}

double FalPos(double xi, double xd, double (*f)(double x))
{
	   double xa;
	   xa= xd-((f(xd)*(xi-xd))/(f(xi)-f(xd)));
	   return xa;
}
	   
double MiFunc(double a, double (*f)(double x))
{
	   double y;
	   y=a*f(a);
	   return y;
}

int main()
{
	double xi, xd, errtol, xap;
	int maxiter, ban, op;
	system("cls");
	printf("Este programa buscara las raices de x*sen(x), introduzca el intervalo\n en la forma de xi,xd: ");
	scanf("%f,%f", &xi, &xd);
	printf("\n\nIntroduzca el error maximo tolerado, valores del 0 al 100: ");
	scanf("%f", &errtol);
	printf("\n\nIntroduzca el numero maximo de iteraciones: ");
	scanf("%i", &maxiter);
	printf("\n\nElija el método, 1 para Bissecion, 2 para Falsa Posicion, otro numero acabara el programa: ");
	scanf("%i", op);
	switch(op)
	{
			  case 1:
				   xap=Raices(xi,xd,errtol,maxiter,&ban,Bisec,MiFunc);
				   break;
			  case 2:
				   xap=Raices(xi, xd, errtol, maxiter, &ban, FalPos, MiFunc);
				   break;
			  default:
					  printf("\n\nFin de Programa\n\n");
					  system("pause");
					  break;
	}
	switch(ban)
	{
			   case 0:
					printf("\n\nNo Existe la raiz en el intervalo\n\n");
					system("pause");
					break;
			   case 1:
					printf("\n\nLa raiz existe, pero el error no es aceptable, pruebe otro metodo:\nRaiz: %f\n\n", xap);
					system("pause");
					break;
			   case 2:
					printf("\n\nLa raiz aproximada es: %f", xap);
					system("pause");
					break;
	 }
}



The issue is when I try to call the functions from main, I can't find how to make the compiler happy. Any help is welcome.

Is This A Good Question/Topic? 1
  • +

Replies To: Bisection and False Position Methods

#2 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: Bisection and False Position Methods

Posted 15 September 2009 - 09:15 PM

Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:

* Post the code you're having problems with
* Post the exact error you're receiving, if you are receiving one
* If no error explain what the code is doing versus what you want it to do
* Post your question in the body of your post, not the description field
* Post your code like this: [code]code goes here [/code]
Was This Post Helpful? 0
  • +
  • -

#3 Sandthief  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 07-June 09

Re: Bisection and False Position Methods

Posted 15 September 2009 - 10:26 PM

For now, it's on line 17, the error says: invalid conversion from `double (*)(double, double (*)(double))' to `double (*)(double)' . Why does this happen? The program doesn't compile and I wrote it on Dev-C++.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1