1stopt8.0 代码示例
具体解释见知乎:https://www.zhihu.com/question/60356140/answer/1280109817
1 NewCodeBlock"1"; 2 Parameter x(1:3)=[0,]; 3 MaxFunction 2*x1+3*x2+x3; 4 x1+3*x2+x3 <= 15; 5 2*x1+3*x2-x3 <= 18; 6 x1-x2+x3 <= 3; 7 NewDivision"2.1"; 8 //直接拟合 9 Variable x,y; 10 Function y=a*x^b; 11 Data; 12 0.091 0.7171 13 0.2543 0.8964 14 0.3121 1.0202 15 0.3792 1.1962 16 0.4754 1.4928 17 0.4410 1.6909 18 0.4517 1.8548 19 0.5595 2.1618 20 0.8080 2.6638 21 NewDivision"2.2"; 22 //线性化后拟合 23 PassParameter a=exp(a1); 24 Variable x1,y1; 25 Function y1=a1+b*x1; 26 Data; 27 -2.3969 -0.3325 28 -1.3692 -0.1094 29 -1.1644 0.0200 30 -0.9697 0.1791 31 -0.7436 0.4007 32 -0.8187 0.5253 33 -0.7947 0.6178 34 -0.5807 0.7709 35 -0.2132 0.9798 36 NewDivision"3"; 37 Algorithm = SM2[ 150]; 38 ParameterDomain = [ 0, ] ; 39 MinFunction -40*x1*Cos(x2)-35*x2*Sin(x1)+15*x1^2-Sin(x1)*20+12*x2^2-25*x1*x2*Sin(x1*x2^2*Cos(x1+x2)); 40 NewDivision"4"; 41 Algorithm = SM2[100]; 42 ParameterDomain = [-50,50]; 43 MinFunction cos(x1)*cos(x2)-Sum(i=1:5)((-1)^i*i*2* 44 exp(-500*((x1-i*2)^2+(x2-i*2)^2))); 45 NewDivision"5.1"; 46 Variable x,y1,y2; 47 SharedModel; 48 Function y1 = a/(1+(b*x)^2); 49 y2 = -(a*c)/(1+(n*x)^2); 50 Data; 51 0 3 -0.789 52 0.01 2.88462 -0.660 53 0.02 2.58621 -0.648 54 0.03 2.20588 -0.511 55 0.04 1.82927 -0.440 56 0.05 1.5 -0.418 57 0.06 1.22951 -0.338 58 0.07 1.01351 -0.279 59 0.08 0.8427 -0.237 60 0.09 0.70755 -0.218 61 0.1 0.6 -0.177 62 0.11 0.5137 -0.136 63 NewDivision"5.2"; 64 ComplexStr = i; 65 Variable x,y[realPart],y[imagPart]; 66 Function y= A/(1+(x*B)^2) - i*(A*C)/(1+(x*B)^2); 67 Data; 68 0 3 -0.789 69 0.01 2.88462 -0.660 70 0.02 2.58621 -0.648 71 0.03 2.20588 -0.511 72 0.04 1.82927 -0.440 73 0.05 1.5 -0.418 74 0.06 1.22951 -0.338 75 0.07 1.01351 -0.279 76 0.08 0.8427 -0.237 77 0.09 0.70755 -0.218 78 0.1 0.6 -0.177 79 0.11 0.5137 -0.136 80 81 NewDivision"6"; 82 Parameter x, y, z; 83 Function (x-0.3)^y^z+x/y/z-x*y*sin(z)+(x+y-z)^cos(x-1) = 1; 84 (y-0.2)^z^x+y/z/x-y*z*sin(x)+(y+z-x)^cos(y-2) = 2; 85 (z-0.1)^x^y+z/x/y-z*x*sin(y)+(z+x-y)^cos(z-3) = 3; 86 NewDivision"7"; 87 Constant n=50, L=100/n; 88 LoopConstant x2(n)=[200, x1(n-1)], y2(n)=[0.3, y1(n-1)]; 89 PlotLoopData y2; 90 function (x2-x1)*y2=l/2*(-4.5*(sin(y2)+sin(y1))+0.02*(sqrt(1+2*x2)*cos(y2)^2+sqrt(1+2*x1)*cos(y1)^2)); 91 (y2-y1)*x2=l/(x2+x1)*(-4.5*(cos(y2)+cos(y1))+2*(sqrt(1+2*x2)*sin(y2)^2+sqrt(1+2*x1)*sin(y1)^2)); 92 NewDivision"8"; 93 Variable t=[0.2 :0.1 :4], y=1.5; 94 Plot t[x], y, y'[y2]; 95 ODEFunction y'=y^(sin(t-y))-ln(y*t); 96 NewDivision"9-分段函数"; 97 Variable t=[0.0:0.1:6], y=0,y'=0; 98 ConstStr f=if(t<=1,t,if(t<=2,2*t,if(t<=5,4,0))); 99 Plot t[x],y,y',y'';//t[x]表示t为横轴x,其余变量绘制在y轴 100 ODEFunction y''=f-2*t*y'-y*t; 101 NewDivision"10-变系数常微分方程求解并绘图"; 102 LoopConstant a=[1:0.02:3]; 103 Variable x=0, y=0, t=[0,2]; 104 Plot x[x],y,y',x'; 105 ODEFunction x'=y-cos(x+y-a)+y*(sin(x^2*y+a)); 106 y'=x-x^3-0.1*y+0.5*cos(0.2*t*y+x-a)*t; 107 NewDivision"11-变边界"; 108 LoopConstant a=[0:0.01:1]; 109 Plot y,y'[x],y''; 110 Variable t=[0,1], y=a, y'=1*a*2; 111 ODEFunction y''=(1-y^2+a)*y'-sin(t*y')*y+cos(t*y'); 112 NewDivision"12-变初值"; 113 LoopConstant a=[-3.5:0.1:3.5]; 114 Variable t=[0:0.2:10],x=2*a,y=a^2; 115 Plot x[x],y, y+x/2; 116 ODEFunction x'=2*(Cos(t)-x+y)/Sqrt((Sin(t)*y-x-0.1)^2+(Cos(t)*x-y)^2); 117 y'=2*(Sin(t)-y+x)/Sqrt((Sin(t)*y-x-0.1)^2+(Cos(t)*x-y)^2); 118 NewCodeBlock"13-微分方程组求参数"; 119 Parameter p1,p2; 120 iValue t=0,x1=p1,x2=0.6,x3=2*p1,x4=p2; 121 SubjectTo x1[1]=x4[1]+p1,x3[0.33]=x4[0.43]; 122 Variable t,x1,x4;//t一定要放x前面,x'就表示dx/dt 123 ODEFunction x1'=x3*x2; 124 x2'=x3*(-x1+sin(x2)); 125 x3'=1; 126 x4'=-2*x1; 127 Data; 128 1,NAN,NAN 129 NewCodeBlock"14-微分方程与代数方程混合拟合"; 130 Variable t,y,z; 131 ODEFunction y''=a*y^2; 132 z=a*ln(t)+b/exp(y); 133 Data; 134 //t y z 135 1 0.909 0.051 136 1.1 0.706 0.276 137 1.2 0.363 0.435 138 1.3 0.079 0.611 139 1.4 -0.226 0.807 140 1.5 -0.474 1.119 141 1.6 -0.765 1.307 142 1.7 -1.183 1.514 143 1.8 -1.436 1.673 144 1.9 -1.573 1.847 145 2 -1.811 2.168 146 2.1 -1.937 2.389 147 2.2 -1.869 2.453 148 2.3 -2.013 2.693 149 2.4 -1.679 2.648 150 2.5 -1.668 2.413 151 2.6 -1.465 2.288 152 2.7 -1.146 2.271 153 2.8 -0.791 2.564 154 2.9 -0.586 2.392 155 3 -0.282 2.197 156 NewCodeBlock"15-微分方程拟合可以设定未知微分方程初值的范围"; 157 Constant u=3.9139*10^(-5); 158 Parameter k1= [0.0001,50]; 159 Parameter k2= [0.0001,1]; 160 Parameter k3= [0.0001,1]; 161 Parameter k4= [0.1,1]; 162 Parameter k5= [0.01,1]; 163 Parameter k6= [0.1,1]; 164 EnhancedBound = 1; 165 InitialODEValue t=1, y1=1.4*10^8,y2=p1=[1.8*10^4,1.4*10^7], y3=p2=[1.8*10^3,1.8*10^5], y4=1212,y5=0; 166 Variable t,y4; 167 ODEFunction 168 y1'=u*(y1+y2+y3+y4+y5)-k1*(y3+y4)*y1/(y1+y2+y3+y4+y5)+k2*y5-u*y1; 169 y2'=k1*(y3+y4)*y1/(y1+y2+y3+y4+y5)-k3*y2-u*y2; 170 y3'= k3*(1-k5)*y2-k4*y3-u*y3; 171 y4'= k3*k5*y2-k6*y4-u*y4; 172 y5'= k4*y3+k6*y4-k2*y5-u*y5; 173 Data; 174 t=31,59,90,120,151,181,212,243,273,304,334,365; 175 y4=37567,23862,77756,248609,354347,343100,261263,119096,101654, 87612,79591,60879; 176 NewCodeBlock"16-共享参数命令 SharedModel2"; 177 Variable x,y; 178 SharedModel2; 179 Function y = a+(b/((c*293-x0-x)+(Sqrt(c*293-x0-x)^2+d*x*293))); 180 y = a+(b/((c*313-x0-x)+(Sqrt(c*313-x0-x)^2+d*x0*313))); 181 Data; 182 1.496 6.893 183 20.940 6.529 184 45.620 5.996 185 75.534 5.338 186 91.239 4.932 187 121.154 4.119 188 150.321 3.222 189 170.513 2.466 190 190.705 1.737 191 219.872 0.658 192 Data; 193 1.496 8.252 194 142.094 6.389 195 191.453 5.450 196 221.368 4.820 197 252.030 4.133 198 281.197 3.363 199 310.363 2.550 200 339.530 1.639 201 NewCodeBlock"17-过指定点"; 202 PassPoint =[2,149], [10,224]; 203 Variable x, y; 204 Parameter m1, m2; 205 Function y=m1*(1-exp(-m2*x)); 206 Data;//x y,一定要和 Variable后的变量对应 207 1 109 208 2 149 209 3 149 210 5 191 211 7 213 212 10 224 213 NewCodeBlock"18-变量值缺失"; 214 Algorithm = SM3; 215 Variable x, y; 216 ParVariable N[-10,10,0]; 217 Function y=k1*x+N*k2; 218 Data; 219 0.2253 0.0047 220 0.2801 0.0431 221 0.3840 -0.0700 222 0.8087 -0.0718 223 NewCodeBlock"19-微分方程中试验数据缺失"; 224 Variable t,x(4); 225 ODEFunction x1'=x3; 226 x2'=x4; 227 x3'=(1-k1)*sin(k2*t+k4)-2*k3*x3+2*k3*x4-x1+x2; 228 x4'=(k1*sin(k2*t+k4)+2*k3*x3-2*k3*(1+k5)*x4+x1-(1+k6)*x2)/k7; 229 Data; 230 0 0.005 -3 0 0 231 1 -1.445 -2.045 -1.914 1.782 232 2 -2.994 NAN -0.346 1.273 233 3 NAN 0.55 1.784 NAN 234 4 0.974 0.576 NAN -0.291 235 5 3.164 NAN 0.789 0.531 236 6 NAN 1.347 -0.862 NAN 237 7 1.346 1.951 NAN 0.676 238 8 0.393 NAN -0.011 -0.903 239 9 NAN 0.808 0.043 NAN 240 10 0.687 -0.57 0.065 -1.363 241 NewCodeBlock"20-拟合输出有范围限制"; 242 Parameters a, b=[,0]; 243 Variable x, y, z; 244 Function z=a*((0.5*(1+b*y)^(1/3))*((1-x)^(2/3)-1)-1/3*(-x)); 245 For(i=1:8,x,y)(a*((0.5*(1+b*y)^(1/3))*((1-x)^(2/3)-1)-1/3*(-x))>=0); 246 data; 247 0.412388 0.171162 2 248 0.513383 0.1624289 4 249 0.598418 0.1455541 6 250 0.69462 0.1123357 10 251 0.768463 0.0908761 15 252 0.833091 0.0789701 20 253 0.893342 0.0571363 30 254 0.912325 0.0474417 45 255 NewCodeBlock"21-批处理拟合---BatchFileModel+VarConstant"; 256 BatchFileModel; 257 VarConstant B = [1,2,3,4]; 258 Variable x = "CodeSheet1[A1:A15]", y = "CodeSheet1[B1:E15]"; 259 Function y = y0+A/(w*sqrt(pi/2))*exp(-(2*(x-x0)^2/w^2))+B; 260 NewCodeBlock"22-变常数拟合---VarConstant"; 261 VarConstant p5=[-1,-0.5,0,0.5]; 262 Function y= p1-p1*exp(-p2*x^p3)+p4*x^p5+p2; 263 Data; 264 0.25,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5,5.5,6,6.5,7,7.5,8,8.5,9; 265 46.3,18.6,3.2,-0.5,-2.0,-2.3,-2.5,-2.3,-2.3,-2.3,-2.4,-2.2,-2.1,-1.8,-1.6,-1.5,-1.3,-1.2,-1.1; 266 Data; 267 0.25,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5,5.5,6,6.5,7,7.5,8,8.5,9; 268 11.7,6.6,2.9,2.3,2.2,2.3,2.4,2.3,2.5,2.6,2.3,2.7,2.5,2.7,3.1,2.9,3.0,3.1,3.0; 269 Data; 270 0.25,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5,5.5,6,6.5,7,7.5,8,8.5,9; 271 -7.3,-2.1,3.1,6.1,7.9,8.7,11.0,11.6,13.2,11.9,14.2,13.7,15.6,14.5,15.1,15.1,15.1,17.0,15.0; 272 Data; 273 0.25,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5,5.5,6,6.5,7,7.5,8,8.5,9; 274 -15.7,-7.4,3.1,10.3,16.7,18.4,26.5,31.0,32.2,31.9,40.4,36.2,40.4,48.8,44.3,50.7,45.9,58.4,55.4; 275 NewCodeBlock"23-变系数拟合--VarParameter"; 276 VarParameter p5; 277 Function y= p1-p1*exp(-p2*x^p3)+p4*x^p5+p2; 278 Data; 279 0.25,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5,5.5,6,6.5,7,7.5,8,8.5,9; 280 46.3,18.6,3.2,-0.5,-2.0,-2.3,-2.5,-2.3,-2.3,-2.3,-2.4,-2.2,-2.1,-1.8,-1.6,-1.5,-1.3,-1.2,-1.1; 281 Data; 282 0.25,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5,5.5,6,6.5,7,7.5,8,8.5,9; 283 11.7,6.6,2.9,2.3,2.2,2.3,2.4,2.3,2.5,2.6,2.3,2.7,2.5,2.7,3.1,2.9,3.0,3.1,3.0; 284 Data; 285 0.25,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5,5.5,6,6.5,7,7.5,8,8.5,9; 286 -7.3,-2.1,3.1,6.1,7.9,8.7,11.0,11.6,13.2,11.9,14.2,13.7,15.6,14.5,15.1,15.1,15.1,17.0,15.0; 287 Data;//老界面少了分号算不了 288 0.25,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5,5.5,6,6.5,7,7.5,8,8.5,9; 289 -15.7,-7.4,3.1,10.3,16.7,18.4,26.5,31.0,32.2,31.9,40.4,36.2,40.4,48.8,44.3,50.7,45.9,58.4,55.4; 290 291 NewCodeBlock"24-高级计算器"; 292 293 f1 = 5*sin(pi*6)^2+exp(6.45+ln(2.14)); 294 f2 = int(x^(sin(Gamma(x)))+(abs(sin(x)))^x, x=0:pi)+sum(i=1:10)(prod(j=1:i)(0.1*ln(i*j)^2)); 295 f3 = sqrt(max(f1^0.1, f2)); 296 f4 = Sum(i=1:3)(Sum(j=1:i)(Sum(m=1:4)(Prod(n=1:m)(Prod(p=1:3)(f3^((i+j+m+n+p)/1000)))))); 297 f5 = Complex(5*sin(pi*6)^2*i+exp(6.45+ln(2.14)*i));//f1,为了避免重合,改了一下名字 298 f6 = Complex(f1+cos(5.6+i));//f2 299 300 Conststr y=x^2+exp(x+2)*sin(x);//定义字符串,下式中左边字符串可以代替右边字符串,所以叫做常字符串Conststr 301 Diff(y,x); 302 Diff(y,x=0.5); 303 Diff(y,x,3); 304 Diff(y,x=0.5,3); 305 NewCodeBlock"25-带积分符号的拟合"; 306 Parameter p(5); 307 Variable x,y; 308 Function y=int(((p1*(x-u)+x^p2)*exp(-p3*(x-u)^2)+p4)^2,u=0,x)/x^p5; 309 Data; 310 0.5,1,1.5,2,2.5,3,3.5,4,4.5,5,5.5,6,6.5,7,7.5,8,8.5,9,9.5; 311 0.063,0.102,0.086,0.075,0.062,0.056,0.051,0.045,0.041,0.040,0.038,0.034,0.030,0.028,0.028,0.028,0.028,0.027,0.024; 312 NewCodeBlock"26-公式自动搜索拟合"; 313 Data; 314 15 0.489 315 30 0.427 316 45 0.373 317 60 0.327 318 75 0.285 319 90 0.250 320 105 0.218 321 120 0.191 322 135 0.167 323 495 0.005