Код программы метода сопряженных направлений
#include <stdio.h>
#include <conio.h>
#include <math.h>
main ()
{
clrscr ();
float x[10];
float y[10];
float Fx,Fy,m1,m2,m;
int i,S1=1,S2=1;
x[0]=0.5;
y[0]=4;
printf("--------METOD SOPR. NAPRAVLENIY--------\n");
printf(" f(x,y)=2x^2+2xy+y^2+2x+2y+0.5\n");
printf("---------------------------------------\n");
for (i=0;i<2;i++)
{
printf(" A%d(%2.3f;%2.3f)\n",i,x[i],y[i]);
printf(" ---------------\n");
Fx=4*x[i]+2*y[i]+2;
Fy=2*x[i]+2*y[i]+2;
printf("S1=%d S2=%d\nFx%d=%2.3f Fy%d=%2.3f\n",S1,S2,i,Fx,i,Fy);
m1=Fx*S1+Fy*S2;
m2=(4*S1+2*S2)*S1+(2*S1+2*S2)*S2;
m=m1/m2;
printf("mu%d=%2.3f\n\n",i,m);
x[i+1]=x[i]-m*S1;
y[i+1]=y[i]-m*S2;
S1=2;
S2=-3; }
printf("---------------------------------------\n");
printf("iskomaya tochka A2(%2.3f;%2.3f)\n",x[2],y[2]);
getch();}
Результат работы программы
--------METOD SOPR. NAPRAVLENIY--------
f(x,y)=2x^2+2xy+y^2+2x+2y+0.5
---------------------------------------
A0(0.500;4.000)
---------------
S1=1 S2=1
Fx0=12.000 Fy0=11.000
mu0=2.300
A1(-1.800;1.700)
---------------
S1=2 S2=-3
Fx1=-1.800 Fy1=1.800
mu1=-0.900
---------------------------------------
iskomaya tochka A2(0.000;-1.000)
Код программы метода симплексных процедур
#include <stdio.h>
#include <conio.h>
#include <math.h>
float fun (float x,float y)
{return(2*x*x+2*x*y+y*y+2*x+2*y+0.5);}
float n1 (float x,float y)
{return(1.5*x+y-2);}
float n2 (float x,float y)
{x=x;
return(y-2);}
float n3 (float x,float y)
{return(x-y-4);}
float n4 (float x,float y)
{x=x;
return(y);}
main (){
clrscr ();
float a=2,b=1,c=1,d=1,e=1,f=0.5;
float na=1.5,nb=1.0,nc=-2.0;
float x,y,z,k,t;
int i,h=1,m;
printf("-----METOD SIMPLEX PROCEDURE-----\n");
printf(" f(x,y)=2x^2+2xy+y^2+2x+2y+0.5\n\n");
printf("1)1.5x+y-2>=0;\n2)y-2<=0;\n3)x-y<=4;\n4)y>=0\n");
printf("----------------------------------\n");
float max=0;
printf("f1(x,y,z)=2x^2+2xy+y^2+2x+2y+0.5+z(1.5x+y-2)\n");
x=((2*b*nc/nb)-2*d-2*c*nc*na+(2*e*na/nb))/(2*a-(2*b*na/nb)-na*2*b/nb+2*c*na*na);
y=(-nc/nb)-(na/nb)*x;
max=fun(x,y);
printf ("x=%2.3f\ny=%2.3f\nA%d(%2.3f;%2.3f)\n",x,y,h,x,y);
if ((n1(x,y)>=0)&(n2(x,y)<=0)&(n3(x,y)<=0)&(n4(x,y)>=0))
{printf("tochka udovl nerav f(A%d)=%2.3f\n",h,fun(x,y));
if (fun(x,y)>max) max=fun(x,y),k=x,t=y,m=h;
else max=max; }
else
printf("tochka ne udovletv neravenstvu\n");
printf("-----------------------------------\n");
na=0.0;nb=1.0;nc=-2.0;h=h+1;
printf("f2(x,y,z)=2x^2+2xy+y^2+2x+2y+0.5+z(y-2)\n");
x=((2*b*nc/nb)-2*d-2*c*nc*na+(2*e*na/nb))/(2*a-(2*b*na/nb)-na*2*b/nb+2*c*na*na);
y=(-nc/nb)-(na/nb)*x;
fun(x,y);
printf ("x=%2.3f\ny=%2.3f\nA%d(%2.3f;%2.3f)\n",x,y,h,x,y);
if ((n1(x,y)>=0)&(n2(x,y)<=0)&(n3(x,y)<=0)&(n4(x,y)>=0))
{printf("tochka udovl nerav f(A%d)=%2.3f\n",h,fun(x,y));
if (fun(x,y)>max) max=fun(x,y),k=x,t=y,m=h;
else max=max;}
else
printf("tochka ne udovletv neravenstvu\n");
printf("------------------------------------\n");
na=1.0;nb=-1.0;nc=-4.0;h=h+1;
printf("f3(x,y,z)=2x^2+2xy+y^2+2x+2y+0.5+z(x-y-4)\n");
x=((2*b*nc/nb)-2*d-2*c*nc*na+(2*e*na/nb))/(2*a-(2*b*na/nb)-na*2*b/nb+2*c*na*na);
y=(-nc/nb)-(na/nb)*x;
fun(x,y);
printf ("x=%2.3f\ny=%2.3f\nA%d(%2.3f;%2.3f)\n",x,y,h,x,y);
if ((n1(x,y)>=0)&(n2(x,y)<=0)&(n3(x,y)<=0)&(n4(x,y)>=0))
{printf("tochka udovl nerav f(A%d)=%2.3f\n",h,fun(x,y));
if (fun(x,y)>max) max=fun(x,y),k=x,t=y,m=h;
else max=max; }
else
printf("tochka ne udovletv neravenstvu\n");
printf("-------------------------------------\n");
na=0.0;nb=1.0;nc=0.0;h=h+1;
printf("f4(x,y,z)=2x^2+2xy+y^2+2x+2y+0.5+z(y)\n");
x=((2*b*nc/nb)-2*d-2*c*nc*na+(2*e*na/nb))/(2*a-(2*b*na/nb)-na*2*b/nb+2*c*na*na);
y=(-nc/nb)-(na/nb)*x;
fun(x,y);
printf ("x=%2.3f\ny=%2.3f\nA%d(%2.3f;%2.3f)\n",x,y,h,x,y);
if ((n1(x,y)>=0)&(n2(x,y)<=0)&(n3(x,y)<=0)&(n4(x,y)>=0))
{printf("tochka udovl nerav f(A%d)=%2.3f\n",h,fun(x,y));
if (fun(x,y)>max) max=fun(x,y),k=x,t=y,m=h;
else max=max; }
else
printf("tochka ne udovletv neravenstvu\n\n");
printf("---------------------------------------------\n");
printf("Nahodim znacheniya funkzii v vershinah figuri\n");
printf("---------------------------------------------\n");
x=0.0;y=2;h=h+1;
printf ("A%d(%2.3f;%2.3f)\nf%d(A%d)=%2.3f\n",h,x,y,h,h,fun(x,y));
printf ("---------------\n");
if (fun(x,y)>max) max=fun(x,y),k=x,t=y,m=h;
else max=max;
x=6.0;y=2;h=h+1;
printf ("A%d(%2.3f;%2.3f)\nf%d(A%d)=%2.3f\n",h,x,y,h,h,fun(x,y));
printf ("---------------\n");
if (fun(x,y)>max) max=fun(x,y),k=x,t=y,m=h;
else max=max;
x=4.0;y=0.0;h=h+1;
printf ("A%d(%2.3f;%2.3f)\nf%d(A%d)=%2.3f\n",h,x,y,h,h,fun(x,y));
printf ("---------------\n");
if (fun(x,y)>max) max=fun(x,y),k=x,t=y,m=h;
else max=max;
x=1.33;y=0;h=h+1;
printf ("A%d(%2.3f;%2.3f)\nf%d(A%d)=%2.3f\n",h,x,y,h,h,fun(x,y));
printf ("-----------------------------------\n");
if (fun(x,y)>max) max=fun(x,y),k=x,t=y,m=h;
else max=max;
printf("Maximalnoe znach funkzii f%d=%2.3f\nA%d(%2.3f;%2.3f)-tochka maximuma\n",m,max,m,k,t);
getch ();}
Результат работы программы
-----METOD SIMPLEX PROCEDURE-----
f(x,y)=2x^2+2xy+y^2+2x+2y+0.5
1)1.5x+y-2>=0;
2)y-2<=0;
3)x-y<=4;
4)y>=0
----------------------------------
f1(x,y,z)=2x^2+2xy+y^2+2x+2y+0.5+z(1.5x+y-2)
x=1.200
y=0.200
A1(1.200;0.200)
tochka udovl nerav f(A1)=6.700
-----------------------------------
f2(x,y,z)=2x^2+2xy+y^2+2x+2y+0.5+z(y-2)
x=-1.500
y=2.000
A2(-1.500;2.000)
tochka ne udovletv neravenstvu
------------------------------------
f3(x,y,z)=2x^2+2xy+y^2+2x+2y+0.5+z(x-y-4)
x=1.200
y=-2.800
A3(1.200;-2.800)
tochka ne udovletv neravenstvu
------------------------------------
f4(x,y,z)=2x^2+2xy+y^2+2x+2y+0.5+z(y)
x=-0.500
y=0.000
A4(-0.500;0.000)
tochka ne udovletv neravenstvu
---------------------------------------------------------
Nahodim znacheniya funkzii v vershinah figuri
---------------------------------------------------------
A5(0.000;2.000)
f5(A5)=8.500
-------------------
A6(6.000;2.000)
f6(A6)=116.500
-------------------
A7(4.000;0.000)
f7(A7)=40.500
-------------------
A8(1.330;0.000)
f8(A8)=6.698
--------------------------------------------------------------------
Maximalnoe znach funkzii f6=116.500
A6(6.000;2.000)-tochka maximuma