⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mtl_ode_sim_eulermethod.m

📁 Eulers Method Ordinary Differential Equations
💻 M
字号:
function mtl_gen_ode_sim_eulermethod
% This file was developed for the Holistic Numerical Methods Institute
% by Nathaniel Collier (http://numericalmethods.eng.usf.edu). Contributions 
% also came from Timothy Fawcett and Dr. Autar Kaw. 
%
% Nothing needs to be changed in the code. Simply run the function (type
% the name in the command window or right click it in the Current Directory
% window and choose run. There a window will appear in which you may alter
% 4 quantities.
%
%   dy/dx in form of f(x,y)
%   x0, x location of known intial condition
%   y0, corresponding value of y at x0
%   xf, x location at where you wish to see the solution to the ODE
%
% These inputs will have defaults, simply change them as needed. Once you
% click away from the text boxes, the screen will automatically update.
% This file will show you Euler's approximation at xf by taking 5 steps to
% get there. Select a segment from the dropdown box to see that segment's
% calculations. 
%
% The conclusion screen shows the exact value compared to the
% approximation. Note that here exact value is a matlab determined exact
% value. The function ode45 is used. There may be some cases where an exact
% solution is not available. This does not mean, however, that the solution
% cannot be approximated or that Euler's is not valid. It simply means that
% there will be no exact value for comparison.

clc; close all;

function inputboxCallback(src,evt)
    
    %Read variables from UI
    fcnstr=get(fcnbox,'String');
    x0=str2num(get(x0box,'String'));
    y0=str2num(get(y0box,'String'));
    xf=str2num(get(xfbox,'String'));
    istep=get(combo,'Value');

    if x0 < xf
        f=inline(fcnstr);
        xspan = [x0 xf];

        % Exact Solution
        [x,y]=ode45(f,xspan,y0);
        plot(x,y,'-','LineWidth',2,...
            'Color',[0 0 1]);

        % Approximations
        if istep >= 2
            h=(xf-x0)/5.0;
            xa=x0;
            ya(1)=y0;
            istepuse=istep;
            if istepuse>=7 %istep corresponds to combo box, here it is adjusted so too many segments do not display in conclusions
                istepuse=6;
            end
            for i = 2:istepuse
                xa(i)=xa(i-1)+h;
                ya(i)=ya(i-1)+f(xa(i-1),ya(i-1))*h;
            end
            hold on
            plot(xa,ya,'-','LineWidth',2,...
                'Color',[0 1 0]);
            hold off
        end

        switch istep
            case 1
                storedtext=sprintf('Introduction:\n\nThe following sheet demonstrates Euler''s method of solving ordinary differential equations. Euler''s method of solving ordinary differential equations uses the derivative and value of the function at the initial condition to project the location and value of the next point on the function.');
            case 2
                storedtext=sprintf('1st Segment:\n\nh= %g\n\nf( x(0) , y(0) ) = %g\n\ny(1) = y(0) + h * f( x(0) , y(0) ) = %g + %g * %g\n\ny(1) = %g',h,f(xa(1),ya(1)),ya(1),h,f(xa(1),ya(1)),ya(2));
            case 3
                storedtext=sprintf('2nd Segment:\n\nf( x(1) , y(1) ) = %g\n\ny(2) = y(1) + h * f( x(1) , y(1) ) = %g + %g * %g\n\ny(2) = %g',f(xa(2),ya(2)),ya(2),h,f(xa(2),ya(2)),ya(3));
            case 4
                storedtext=sprintf('3rd Segment:\n\nf( x(2) , y(2) ) = %g\n\ny(3) = y(2) + h * f( x(2) , y(2) ) = %g + %g * %g\n\ny(3) = %g',f(xa(3),ya(3)),ya(3),h,f(xa(3),ya(3)),ya(4));
            case 5
                storedtext=sprintf('4th Segment:\n\nf( x(3) , y(3) ) = %g\n\ny(4) = y(3) + h * f( x(3) , y(3) ) = %g + %g * %g\n\ny(4) = %g',f(xa(4),ya(4)),ya(4),h,f(xa(4),ya(4)),ya(5));
            case 6
                storedtext=sprintf('5th Segment:\n\nf( x(4) , y(4) ) = %g\n\ny(5) = y(4) + h * f( x(4) , y(4) ) = %g + %g * %g\n\ny(5) = %g',f(xa(5),ya(5)),ya(5),h,f(xa(5),ya(5)),ya(6));
            case 7
                [yfi dummy]=size(y);
                yf=y(yfi);
                storedtext=sprintf('Conclusions:\nWhile Euler''s method is valid for approximating the solutions of ordinary differential equations, the use of the slope at one point to project the value at the next point is not very accurate. Note the values obtained as well as the true and relative true error at our desired point x = xf.\n\nExact Value    ye(xf) = %g\nApproximate Value    ya(5) = %g\n\nTrue Error    ye(xf) - ya(5) = %g - %g = %g\nAbsolute Relative True Error Percentage    ( ye(xf) - ya(5) ) / ye(xf) *100 = %g / %g = %g\n\n That is a lot of error! Here, our approximation would be better if we made our segments smaller and more in number. Other methods use the same principle as Euler''s method: use some approximation of slope to find the value at a point a fixed distance away. The difference in methods is the function used to approximate the slope.',yf,ya(6),yf,ya(6),yf-ya(6),yf-ya(6),yf,(yf-ya(6))/yf*100);
            case 8
                 storedtext=sprintf('References:\n\n[1] Autar Kaw, Michael Keteltas, Holistic Numerical Methods Institute\n\n\nDisclaimer:  \nWhile every effort has been made to validate the solutions in this worksheet, University of South Florida and the contributors are not responsible for any errors contained and are not liable for any damages resulting fromt the use of this material.');
        end
        set(introbox,'String',storedtext);
    else
        warndlg('For Matlab''s solver to work correctly, you must have x0 < xf. Please change these values and try again.');
    end %end if
end % End inputboxCallback

function fontCallback(src,evt)
    
    fs=6+2*(get(combof,'Value')-1);
    set(titlebox,'FontSize',fs+4);
    set(fcnlabel,'FontSize',fs);
    set(fcnbox,'FontSize',fs);
    set(y0label,'FontSize',fs);
    set(x0box,'FontSize',fs);
    set(y0box,'FontSize',fs);
    set(xflabel,'FontSize',fs);
    set(introbox,'FontSize',fs);    
    set(xfbox,'FontSize',fs);
    
end % End fontCallback

function helpme(src,evt)
    
    web http://numericalmethods.eng.usf.edu/mtlhelp/08ode/mtl_gen_ode_hlp_eulermethod.doc -browser;
    
end % End fontCallback

function gotoweb(src,evt)
    
    web http://numericalmethods.eng.usf.edu/topics/euler_method.htm -browser;
    
end % End fontCallback

%GUI----------------------------------------------------------------



% Window
ssize=get(0,'ScreenSize');
gsize=[1/8*ssize(3) 1/8*ssize(4) 3/4*ssize(3) 3/4*ssize(4)];
gui=figure('Resize','off',...
    'Position',gsize,...
    'Name','Euler''s Method of Solving Ordinary Differential Equations',...
    'NumberTitle','off',...
    'menubar','none');

% !!!Set font sizes
switch ssize(3) 
case 800
    fs = 6;
    sel = 1;
    warndlg('At this resolution (800 x 600) you may have trouble viewing all data in this window.');
case 1024
    fs = 8;
    sel = 2;
case 1152
    fs = 8;
    sel = 2;
case 1280
    fs = 8;
    sel = 2;
case 1600
    fs = 10;
    sel = 3;
otherwise
    fs = 8;
    sel = 3;
end

panelColor = get(gui,'Color');

% Title box
title_pos=[1/16*gsize(3) 29/32*gsize(4) 7/8*gsize(3) 2/32*gsize(4)];
title = 'Euler''s Method of Solving Ordinary Differential Equations';
titlebox=uicontrol(gui,'BackgroundColor',panelColor,...
    'Style','text',...
    'FontWeight','bold',...
    'String',title,...
    'FontSize',fs+4,...
    'Position',title_pos);

% Label for Function Input
fcnlabel_pos=[1/16*gsize(3) 3/32*gsize(4) 7/8/3*gsize(3) 1/32*gsize(4)];
fcnlabel=uicontrol(gui,'BackgroundColor',[1 1 1],...
    'Style','text',...
    'String','Enter dy/dx as f(x,y)',...
    'FontSize',fs,...
    'Position',fcnlabel_pos);

% Function Input Box
fcnbox_pos=fcnlabel_pos+[fcnlabel_pos(3) 0 -7/8/6*gsize(3) 0];
fcnbox=uicontrol(gui,'BackgroundColor',[1 1 1],...
    'Style','edit',...
    'String','y*x^2-1.2*y',...
    'FontSize',fs,...
    'Position',fcnbox_pos,...
    'Callback',@inputboxCallback);

% Label for x0 and y0 Input
y0label_pos=[1/16*gsize(3) 2/32*gsize(4) 7/8/3*gsize(3) 1/32*gsize(4)];
y0label=uicontrol(gui,'BackgroundColor',[1 1 1],...
    'Style','text',...
    'String','Initial condition x0 and y0 repsectively',...
    'FontSize',fs,...
    'Position',y0label_pos);

% x0 Input box
x0box_pos=y0label_pos+[y0label_pos(3) 0 -7/8/6*gsize(3)-fcnbox_pos(3)/2 0];
x0box=uicontrol(gui,'BackgroundColor',[1 1 1],...
    'Style','edit',...
    'String','0.0',...
    'FontSize',fs,...
    'Position',x0box_pos,...
    'Callback',@inputboxCallback);

% y0 Input Box
y0box_pos=x0box_pos+[x0box_pos(3) 0 0 0];
y0box=uicontrol(gui,'BackgroundColor',[1 1 1],...
    'Style','edit',...
    'String','1.0',...
    'FontSize',fs,...
    'Position',y0box_pos,...
    'Callback',@inputboxCallback);

% Label for xf
xflabel_pos=[1/16*gsize(3) 1/32*gsize(4) 7/8/3*gsize(3) 1/32*gsize(4)];
xflabel=uicontrol(gui,'BackgroundColor',[1 1 1],...
    'Style','text',...
    'String','Enter x at which solution is desired, xf',...
    'FontSize',fs,...
    'Position',xflabel_pos);

% xf Input box
xfbox_pos=xflabel_pos+[xflabel_pos(3) 0 -7/8/6*gsize(3) 0];
xfbox=uicontrol(gui,'BackgroundColor',[1 1 1],...
    'Style','edit',...
    'String','2.0',...
    'FontSize',fs,...
    'Position',xfbox_pos,...
    'Callback',@inputboxCallback);

% Intro Frame and Text Box
intro_frame_pos=[2/32*gsize(3) 40/64*gsize(4) 14/16*gsize(3) 19/64*gsize(4)];
intro_frame = uicontrol(gui,'BackgroundColor',[1 1 1],...
    'Position',intro_frame_pos);
introbox = uicontrol(gui,'BackgroundColor',[1 1 1],...
    'Position',intro_frame_pos+[20 20 -40 -40],...
    'Style','text',...
    'FontSize',fs,...
    'HorizontalAlignment', 'left',...
    'String','');

% Combo box for display
combo_pos = fcnlabel_pos + [fcnlabel_pos(3)+fcnbox_pos(3)+x0box_pos(3) 0 0 0];
combo_sel = {'Introduction and Exact Solution',...
    '1st Segment',...
    '2nd Segment',...
    '3rd Segment',...
    '4th Segment',...
    '5th Segment',...
    'Conclusions',...
    'References'};
combo = uicontrol(gui,'BackgroundColor',[1 1 1],...
    'Style','popupmenu',...
    'position',combo_pos,...
    'String',combo_sel,...
    'Callback',@inputboxCallback);

% !!! Combo box for fonts sizes
combof_pos = combo_pos - [0 combo_pos(4) 0 0];
combof_sel = {'FontSize 6',...
    'FontSize 8',...
    'FontSize 10',...
    'FontSize 12',...
    'FontSize 14'};
combof = uicontrol(gui,'BackgroundColor',[1 1 1],...
    'Style','popupmenu',...
    'position',combof_pos,...
    'String',combof_sel,...
    'Value',sel,...
    'Callback',@fontCallback);

% Help Button
help_pos=combof_pos+[0 -combof_pos(4) -0.5*combof_pos(3) 0];
helpbutton = uicontrol(gui,'BackgroundColor',[1 1 1],...
    'Position',help_pos,...
    'String','Help!',...
    'Callback',@helpme);

% Web Button
web_pos=help_pos+[help_pos(3) 0 0 0];
webbutton = uicontrol(gui,'BackgroundColor',[1 1 1],...
    'Position',web_pos,...
    'String','More Resources',...
    'Callback',@gotoweb);

plot_pos=[1/16 5/32 7/8 28/64];
plot1=axes('position',plot_pos,...
    'FontSize',fs);


inputboxCallback %This runs the callback function immediately after loading all GUI items

end % end eulermethod





⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -