# 无约束问题极值的求解及数学建模算法与应用第三章

>> [x,fval]=fminunc(@(x)x(1)^3-x(2)^3+3*x(1)^2+3*x(2)^2-9*x(1),zeros(2,1))
x =

1.0000
-0.0000

fval =

-5


>> [x,fval]=fminunc(@(x)100*(x(2)-x(1)^2)^2+(1-x(1))^2,zeros(2,1))
x =

1.0000
1.0000

fval =

1.9474e-11
y值相当于是0了


>>  [x,fval]=fminunc(@(x)sin(x)+3,zeros(1,1))
x =

-1.5708

fval =

2



vpa(solve('x^3-x^2+2*x-3=0','x'))
%vpa是为了让答案为小数而不是分数


x^2+y-6=0

y^2+x-6=0

[x,y]=solve('x^2+y-6=0','y^2+x-6=0','x','y')

>> [x,fval]=fminunc(@(x)(x-3)^2-1,zeros(1,1))
x =

3

fval =

-1


3.1

miny=50x1+0.2x1^2+20x2+0.2x2^2+50x3+0.2x3^3+(x1-40)*4+(x2+x1-100)*4

40<=x1<=100

60<=x2<=100

80<=x3<=100

x1+x2+x3=180

x1,x2,x3为整数

非线性整数规划问题一般可以考虑蒙特卡洛方法

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
int check(int x[])
{
int i,j;
if (x[1]<40)
return 0;
if (x[1]+x[2]<100)
return 0;
if (x[3]<0)
return 0;
return 1;

}
int main()
{
int ans[4];
int min;
int x[4];
int i,j;
srand(time(NULL));
min=999999999;
for (i=1;i<=100000;i++)
{
x[1]=rand()%101;
x[2]=rand()%101;
x[3]=180-x[1]-x[2];
if (check(x))
if (50*x[1]+0.2*x[1]*x[1]+50*x[2]+0.2*x[2]*x[2]+50*x[3]+0.2*x[3]*x[3]+(x[1]-40)*4+(x[2]+x[1]-100)*4<min)
{
for (j=1;j<=3;j++)
ans[j]=x[j];
min=50*x[1]+0.2*x[1]*x[1]+50*x[2]+0.2*x[2]*x[2]+50*x[3]+0.2*x[3]*x[3]+(x[1]-40)*4+(x[2]+x[1]-100)*4;
}
}
printf("最大值为:%d\n",min);
for (j=1;j<=3;j++)
printf("%d ",ans[j]);

}

3.4

fun4.m

function f=fun4(x);
f=-2*x(1)-3*x(1)^2-3*x(2)-x(2)^2-x(3);


fun5.m

function [g,h]=fun5(x)
g(1)=x(1)+2*x(1)^2+x(2)+2*x(2)^2+x(3)-10;
g(2)=x(1)+x(1)^2+x(2)+x(2)^2-x(3)-50;
g(3)=2*x(1)+x(1)^2+2*x(2)+x(3)-40;
g(4)=-x(1)-2*x(2)+1;
h=x(1)^2+x(3)-2;


[x,y]=fmincon('fun4',[-1 -2 0],[],[],[],[],(0),[],'fun5')

x =

2.3333    0.1667   -3.4444

y =

-18.0833