当前位置:首页 >> 理学 >>

打靶法+MATLAB程序


function dy=shootingfun(t,y); % 定义打靶法的微分方程 % y''+ty'-4y=12t^2-3t 0<t<1 % y(0)=0,y(1)=2 dy=[y(2);4*y(1)-t.*y(2)+12*t.^2-3*t];
复制内容到剪贴板 代码:

% 关于线性打靶法的 GUI 文件,函数方程用 shootingfun.m 文件 close all figure; axes('position',[0.06,0.16,0.7,0.6]); p='y''(0)=';P='\Delta(y(1))';Q=[]; Sl=uicontrol(gcf,'style','slider',... 'unit','normalized','position',[0.93,0.02,0.03,0.9],... 'BackgroundColor',[0.9 0.9 0.9],'ForegroundColor','r',... 'fontsize',14,'SliderStep',[0.01,0.01]); set(Sl,'callback',['a=str2num(get(Ed1,''string''));',... 'b=str2num(get(Ed0,''string''));',... 'y0=b+(a-b)*get(Sl,''value'');q=num2str(y0);',... 'set(Te,''string'',[p,q]);eval(SS);']); Te=uicontrol(gcf,'style','text',... 'unit','normalized','position',[0.78,0.92,0.24,0.05],... 'BackgroundColor','w','ForegroundColor','r',... 'fontsize',10,'string',['y''(0)=',... num2str(get(Sl,'value'))]); Ed1=uicontrol(gcf,'style','edit',... 'unit','normalized','position',[0.81,0.86,0.1,0.05],... 'BackgroundColor','w','ForegroundColor','r',... 'string','2'); Ed0=uicontrol(gcf,'style','edit',... 'unit','normalized','position',[0.81,0.02,0.1,0.05],... 'BackgroundColor','w','ForegroundColor','r',... 'string','0'); Te0=uicontrol(gcf,'style','text',... 'unit','normalized','position',[0.11,0.8,0.4,0.2],... 'BackgroundColor',[0.8 0.8 0.9],'ForegroundColor','b',... 'string',{'differential equations:',... 'y''''+ty''-4y=12t^2-3t 0<t<1','y(0)=0,y(1)=2'},... 'fontsize',14,'HorizontalAlignment','left'); plot([0,1],[0,2],'r*');hold on;plot([0,1],[0,2],'rs'); SS=['[t,y]=ode45(@shootingfun,[0,1],[0,y0]);',... 'set(h,''xdata'',t);set(h,''ydata'',y(:,1));',... 'set(DD,''string'',{P,num2str(y(end,1)-2)});'];

h=plot([0,1],[0,0]); DD=uicontrol(gcf,'style','text',... 'unit','normalized','position',[0.55,0.8,0.18,0.14],... 'BackgroundColor','w','ForegroundColor','r',... 'string',{P,Q});


相关文章:
更多相关标签: