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

📄 simplicityalogrithm.c

📁 /*用单纯形算法求解多元函数极值计算机程序代码*/ /************************ *试用单纯形法求目标函数: *f(x)= 4(x1 -5)*(x1 -5) + (x2
💻 C
字号:
/*用单纯形算法求解多元函数极值计算机程序代码*/

/************************
*试用单纯形法求目标函数:
*f(x)= 4(x1 -5)*(x1 -5) + (x2 -6)*(x1 -5)
*的极小值
*************************/

# include "math.h"
# include "stdio.h"

float d1 = 1, d2 = 0.5, d3 = 2, dd = 0.000001;
float f(a1, a2)
float a1, a2;
{
	float y1;
	y1 = 4 * (a1 - 5) * (a1 - 5) + (a2 - 6) * ( a2 - 6);
	return (y1);
}

float f2(a3, a4, a5 , n)
float a3, a4, a5;
int n;
{
	float t;
	if (a3 >= a4)
		{
			t = a3;	a3 = a4; a4 = t;
		}
		if(a3 >= a5)
			{
				t = a3;	a3 = a5; t = a3;
			}
			if (a4 >= a5)
				{
					t = a4;	a4 = a5;	a5 = t;
				}
				if(n ==1)	return(a3);
					if(n == 2) return(a4);
						return(a5);
}
main()
{
	float f1, fg, fh, xh1, xh2, x11, x12, xg1, xg2, d, s1, s2, s3, d4;
	static float x[10][2] = {{8,9},{10,11},{8,11}};
	int h, g, l, j, m, k = 0;
	for(j = 0; ; j++)
	{
		f1 = f2(f(x[0][0], x[0][1]),f(x[1][0], x[1][1]),f(x[2][0], x[2][1]),1);
		fg = f2(f(x[0][0], x[0][1]),f(x[1][0], x[1][1]),f(x[2][0], x[2][1]),2);
		fh = f2(f(x[0][0], x[0][1]),f(x[1][0], x[1][1]),f(x[2][0], x[2][1]),3);
		for(h = 0; h <= 2; h++)
		{
			if(fh == f(x[h][0],x[h][1]))
				{
					xh1 = x[h][0];
					xh2 = x[h][1];
					break;
				}
		}
		for (g = 0; g <= 2; g++)
		{
			if(fg == f(x[g][0], x[g][1]))
				{
					xg1 = x[g][0];
					xg2 = x[g][1];
					break;
				}
		}
		for(l = 0; l <= 2; l++)
		{
			if(f1 == f(x[1][0], x[1][1]))
				{
					x11 = x[1][0];
					x12 = x[1][1];
					break;
				}
		}
			x[3][0] = (x[0][0] + x[1][0] + x[2][0] - xh1)/2;
			x[3][1] = (x[0][1] + x[1][1] + x[2][1] - xh1)/2;
			x[4][0] = x[3][0] + d1 * (x[3][0] - xh1);
			x[4][1] = x[3][1] + d1 * (x[3][1] - xh2);
			if(f(x[4][0],x[4][1]) < f(x11, x12))
				{
					x[5][0] = x[3][0] + 2 * (x[4][0] - x[3][0]);
					x[5][1] = x[3][1] + 2 * (x[4][1] - x[3][1]);
					if(f(x[5][0], x[5][1]) < f1)
						{
							x[2][0] = x[5][0];
							x[2][1] = x[5][1];
						}
						else
							{
								x[h][0] = x[4][0];
								x[h][1] = x[4][1];
							}
				}
				else
					{
						if(f(x[4][0], x[4][1]) > fg)
							{
								if(f(x[4][0] ,x[4][1]) < fh)
									{
										x[h][0] = x[4][0];
										x[h][1] = x[4][1];
										x[6][0] = x[3][0] + 0.5 * (x[h][0] - x[3][0]);
										x[6][1] = x[3][1] + 0.5 * (x[h][1] - x[3][1]);
										if(f(x[6][0], x[6][1]) > f(x[h][0], x[h][1]))
											{
												x[0][0] = x11 + (x[0][0] - x[1][0])/2;
												x[0][1] = x12 + (x[0][1] - x[1][1])/2;
												x[1][0] = x11 + (x[1][0] - x[1][0])/2;
												x[0][1] = x12 + (x[1][1] - x[1][1])/2;
												x[2][0] = x11 + (x[2][0] - x[1][0])/2;
												x[2][1] = x12 + (x[2][1] - x[1][1])/2;
											}
											else
												{
													x[h][0] = x[6][0];
													x[h][1] = x[6][1];
												}
									}
									else
										{
								  	 x[6][0] = x[3][0] + 0.5 * (x[h][0] - x[3][0]);
								   	 x[6][1] = x[3][1] + 0.5 * (x[h][1] - x[3][1]);	
								   	 if(f(x[6][0], x[6][1]) > f(x[h][0], x[h][1]))
											{
												x[0][0] = x11 + (x[0][0] - x[1][0])/2;
												x[0][1] = x12 + (x[0][1] - x[1][1])/2;
												x[1][0] = x11 + (x[1][0] - x[1][0])/2;
												x[0][1] = x12 + (x[1][1] - x[1][1])/2;
												x[2][0] = x11 + (x[2][0] - x[1][0])/2;
												x[2][1] = x12 + (x[2][1] - x[1][0])/2;
											}
											else
												{
													x[h][0] = x[6][0];
													x[h][1] = x[6][1];
												}
									}	
								}
								else
									{
										x[h][0] = x[4][0];
										x[h][1] = x[4][1];
									}
								}
								s1 = pow(f(x[0][0], x[0][1]) - f(x[3][0], x[3][1]),2);	
								s2 = pow(f(x[2][0], x[2][1]) - f(x[3][0], x[3][1]),2);
								s3 = pow(f(x[1][0], x[1][1]) - f(x[3][0], x[3][1]),2);
								d = sqrt((s1 + s2 + s3)/3);
								d4 = f(x[1][0], x[1][1]);
								if (d <= dd)
									{
										printf("xmin1 = %f, xmin2 = %f, f(x)min + %f, \n ",x[1][0],x[1][1],d4);
									}
									else 
										k = k + 1;
									}
									getch();
								
}

⌨️ 快捷键说明

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