赞
踩
- ode = @(t,y) -abs(y);
- % Standard solution with |ode45|
- options1 = odeset('Refine',1);
- [t0,y0] = ode45(ode,[0 40],1,options1);
- % Solution with nonnegative constraint
- options2 = odeset(options1,'NonNegative',1);
- [t1,y1] = ode45(ode,[0 40],1,options2);
- % Analytic solution
- t = linspace(0,40,1000);
- y = exp(-t);
- % 绘制这三个解进行比较。施加非负约束对于防止解向 − ࣛ 发展至关重要。
- plot(t,y,'b-',t0,y0,'ro',t1,y1,'k*');
- legend('Exact solution','No constraints','Nonnegativity', ...
- 'Location','SouthWest')
运行结果如下:
- epsilon = 1e-6;
- y0 = 1;
- xspan = [0 2];
- odefcn = @(x,y,epsilon) ((1-x)*y - y^2)/epsilon;
- % Solve without imposing constraints
- [x1,y1] = ode15s(@(x,y) odefcn(x,y,epsilon), xspan, y0);
- % Impose a nonnegativity constraint
- options = odeset('NonNegative',1);
- [x2,y2] = ode15s(@(x,y) odefcn(x,y,epsilon), xspan, y0, options);
- % 绘制解进行比较。
- plot(x1,y1,'ro',x2,y2,'b*')
- axis([0,2,-1,1])
- title('The "knee problem"')
- legend('No constraints','Non-negativity')
- xlabel('x')
- ylabel('y')
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。