编写程序如下:
x = (0: 5)
y = rand(1,6)
p = polyfit(x,y,3)%p是多项式系数
f = polyval(p,x)
plot(x,y,'o',x,f,'-')
一、x=[1.75,2.25,2.5,2.875,2.686,2.563]y=[0.26,0.32,0.44,0.57,0.50,0.46]plot(x,y,'g.','markersize',25)%%%有这个是先描点,看出大致是什么图,这里看到像二次曲线的。hold on p3=polyfit(x,y,2)。
二、同理x=[0.26,0.32,0.44,0.57,0.50,0.46]y=[1.75,2.25,2.5,2.875,2.686,2.563]plot(x,y,'g.','markersize',25)hold on p3=polyfit(x,y,1)x2=0:0.5:2y2=3.3237*x2+1.0247plot(x2,y2,'b')。
三、代码:clear allclcclose allx=[1.75,2.25,2.5,2.875,2.686,2.563]y=[0.26,0.32,0.44,0.57,0.50,0.46]plot(x,y,'r*')[p,s]=polyfit(x,y,5) %参数改为1就是线性拟合y1=polyval(p,x)hold onplot(x,y1,'b.')五次多项式拟合生成的函数值与原数据基本重合。
>>clear>>f=inline('a(1)*x+a(2)*x.^2.*exp(-a(3)*x)+a(4)','a','x')
x=[0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1]
y=[2.3201 2.6470 2.9707 3.2885 3.6008 3.9090 4.2147 4.5191 4.8232 5.1275]
[xx,res]=lsqcurvefit(f,ones(1,4),x,y)
xx',res
要建立也是可以的。就是把上面那个inline弄成.m
如下:
在Matlab下输入:edit zhidao_15.m,然后将下面两行百分号之间的内容,复制进去,保存
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function y=zhidao_15(a,x)
y=a(1)*x+a(2)*x.^2.*exp(-a(3)*x)+a(4)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
在Matlab下面输入:
x=[0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1]
y=[2.3201 2.6470 2.9707 3.2885 3.6008 3.9090 4.2147 4.5191 4.8232 5.1275]
[xx,res]=lsqcurvefit('zhidao_15',ones(1,4),x,y)
xx',res
MATLAB代码如下:
c=ones(1,7)A=[0 0 0 0 1 1 2
0 1 2 3 0 1 0
6 4 2 0 4 1 1]
A=-A
b=-100*ones(3,1)
lb=zeros(1,7)
[x,fval,exitflag]=linprog(c,A,b,[],[],lb)
优化结果:
Optimization terminated.
x =
0.0000
0.0000
0.0000
33.3333
14.2857
0.0000
42.8571
fval =
90.4762
exitflag =
1
结果收敛。
1stopt代码:
Parameter x(1:7)[0,]MinFunction x1+x2+x3+x4+x5+x6+x7
x5+x6+2*x7>=100
x2+2*x3+3*x4+x6>=100
6*x1+4*x2+2*x3+4*x5+x6+x7>=100
优化结果:
迭代数: 94
计算用时(时:分:秒:毫秒): 00:00:01:312
计算中止原因: 达到收敛判定标准
优化算法: 准牛顿法(BFGS) + 通用全局优化法
函数表达式: x1+x2+x3+x4+x5+x6+x7
目标函数值(最小): 90.4789793976423
x1: 6.56531036168569E-22
x2: 0.0290946768006313
x3: 4.66048571470512E-16
x4: 33.3235799630505
x5: 14.252439546946
x6: 0.00016543404783297
x7: 42.8736997767972
约束函数
1: x5+x6+2*x7-(100) = 4.534588371E-6
2: x2+2*x3+3*x4+x6-(100) = 0
3: 6*x1+4*x2+2*x3+4*x5+x6+x7-(100) = 2.105831783E-6
====== 计算结束 ======
两种方式下计算一致,解可信。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)