⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bisection.cpp

📁 This program contains the iterative methods of finding solutions of the equation f(x)=0, which are a
💻 CPP
字号:
#include "stdio.h"
#include "math.h"
#include "conio.h"
#include "iostream.h"
#include "stdlib.h"
#include "time.h"

int a[100],i,n;
float x0,x1;
double v0,v1;
double fx(double);

void poleva()
{
	int n, a[100],i,x,p,j;

	printf("POLYNOMIAL EVALUATION \n\n");
	printf("Input the degree= ");
	scanf("%d",&n);

	for(i=0;i<=n;i++)
	{
		printf("a[%d]= ",i);
		scanf("%d",&a[i]);
	}
	printf("The equation is ");
	for (i=n;i>=0;i--)
	{
		if(a[i]!=0)
		{
			printf("%d",a[i]);
			if(i!=0)
			{
				printf("x");
				if(i!=1)
				{
					printf("^%d",i);
				}

			}
			if(i>0)
			{
				if(a[i-1]>0)
				{
					printf("+");
				};
			}
		}
	}
	getch();
	printf("\nValue of x= ");
	scanf("%d",&x);
	p=a[n];
	printf("p when the degree %d = %d\n",n,p);
	for(i=n;i>=1;i--)
	{
	      j=i-1;
	      p=a[j]+x*p;
	      printf("p when the degree %d = %d\n",i,p);
	}
	printf("\nSo the value of the equation is %d",p);
	getch();
}

void create_function()
{
	printf("Input the degree= ");
	scanf("%d",&n);

	//showing the rule of coeficient
	printf("\nFollow this rule of coeficient:\n");
	for (i=n;i>=0;i--)
	{
			printf("a[%d]",i);
			if(i!=0)
			{
				printf("x");
				if(i!=1)
				{
					printf("^%d",i);
				}

			}
			if(i>0)
			{
				printf("+");
				
			}
	}
	getch();
	printf("\n\nEntering the coeficient:\n");
	//inputting the coeficient
	for(i=0;i<=n;i++)
	{
		printf("a[%d]= ",i);
		scanf("%d",&a[i]);
	}
	//printing the equation
	printf("The equation is ");
	for (i=n;i>=0;i--)
	{
		if(a[i]!=0)
		{
			printf("%d",a[i]);
			if(i!=0)
			{
				printf("x");
				if(i!=1)
				{
					printf("^%d",i);
				}

			}
			if(i>0)
			{
				if(a[i-1]>0)
				{
					printf("+");
				};
			}
		}
	}
	getch();
}
double fx(double x)
{
	double result=0;
	for(i=0;i<=n;i++)
	{  
		result=result+a[i]*pow(x,i);

	}
	return result;
}
double def_fx(double x)
{
	double result=0,temp;
	int j;
	for(i=1;i<=n;i++)
	{
		j=i-1;
		if(j==0)
		{
			temp=a[i]*pow(x,j);
		}
		else
		{
			temp=a[i]*pow(x,j)*i;
		}
		result=result+temp;
	}
	return result;
}
float absolute(float num)
{
if (num < 0)
return (-num);
else
return (num);
}


void bisection()
{
	float mid,dmid;
	double val;
	int flag=0;
	printf("BISECTION METHOD\n\n");
	create_function();
	x0=1;
	v0=fx(x0);
	x1=2;
	while(flag==0)
	{
		v1=fx(x1);
		if(v0*v1>0)
		{
			flag=0;
		}
		else
		{
			flag=1;
		}
		x1++;
	}
	x1--;
	printf("\nSo the x0 = %f and x1 = %f\n\n",x0,x1);
	getch();	
	printf(":: Bisection Calculation ::\n\n");
	do {
		mid=(x0+x1)/2;
		val=fx(mid);
		printf ("Middle = %f, F(Middle) = %f\n",mid,val);
		if(val<0)
		{
			x0=mid;
		}
		else
		{
			x1=mid;
		}
		dmid=x1-x0;
		printf("x0 = %f and x1= %f\n\n",x0,x1);
	}
	while(dmid>0.00001);
	printf("So the real root is %.4f \n",mid); 
}

void false_position()
{
	float mid,dmid;
	double val;
	int flag=0;
	printf("FALSE POSITION METHOD\n\n");
	create_function();
	x0=1;
	v0=fx(x0);
	x1=2;
	while(flag==0)
	{
		v1=fx(x1);
		if(v0*v1>0)
		{
			flag=0;
		}
		else
		{
			flag=1;
		}
		x1++;
	}
	x1--;
	printf("\nSo the x0 = %f and x1 = %f\n\n",x0,x1);
	getch();	
	printf(":: False Position Calculation ::\n\n");
	do {
		mid=(x0*fx(x1)-x1*fx(x0))/(fx(x1)-fx(x0));
		val=fx(mid);
		printf ("Middle = %f, F(Middle) = %f\n",mid,val);
		if(val<0)
		{
			x0=mid;
		}
		else
		{
			x1=mid;
		}
		dmid=x1-x0;
		printf("x0 = %f and x1= %f\n\n",x0,x1);
	}
	while(dmid>0.00001);
	printf("So the real root is %.4f \n",mid); 
}

void secant()
{
	float mid,dmid;
	double val;
	int flag=0;
	printf("SECANT METHOD\n\n");
	create_function();
	x0=1;
	v0=fx(x0);
	x1=2;
	while(flag==0)
	{
		v1=fx(x1);
		if(v0*v1>0)
		{
			flag=0;
		}
		else
		{
			flag=1;
		}
		x1++;
	}
	x1--;
	printf("\nSo the x0 = %f and x1 = %f\n\n",x0,x1);
	getch();	
	printf(":: Secant Calculation ::\n\n");
	do {
		mid=(x0*fx(x1)-x1*fx(x0))/(fx(x1)-fx(x0));
		val=fx(mid);
		printf ("Middle = %f, F(Middle) = %f\n",mid,val);
		x0=x1;
		x1=mid;
		dmid=x1-x0;
		//printf("%f",dmid);
		dmid=absolute(dmid);
		
		printf("x0 = %f and x1= %f\n\n",x0,x1);

	}
	while(dmid>0.00001);
	printf("So the real root is %.4f \n",mid); 
}

void newton_raphson()
{
	float mid,dmid,guess;
	int flag=0;
	printf("NEWTON RAPHSON METHOD\n\n");
	create_function();
	x0=1;
	v0=fx(x0);
	x1=2;
	while(flag==0)
	{
		v1=fx(x1);
		if(v0*v1>0)
		{
			flag=0;
		}
		else
		{
			flag=1;
		}
		x1++;
	}
	x1--;
	printf("\nSo the x0 = %f and x1 = %f\n\n",x0,x1);
	getch();
	printf("Please input number between x0 and x1! ");
	scanf("%f",&guess);
	x0=guess;
	printf("\n\n:: Newton Raphson Calculation ::\n\n");
	do {
		x1=x0-fx(x0)/def_fx(x0);
		printf ("Next root = %f\n",x1);
		dmid=x1-x0;
		//printf("%f",dmid);
		dmid=absolute(dmid);
		x0=x1;
	}
	while(dmid>0.00001);
	printf("So the real root is %.4f \n",x1);
	//dmid=def_fx(guess);
	//printf("%f",dmid);
}

void main()
{
	//bisection();
	//false_position();
	//secant();
	//newton_raphson();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -