You are on page 1of 20

2/12/2012

S162109

PRE MASTERS COURSE - MATLAB ASSIGNMENT

Computing Course | Joseph Timoney

Introduction:What is Eulers Method? Eulers Method is the simplest method in solving Ordinary Differential Equations using a given initial value and is the most basic form of the Runge-Kutta Method. Formula:YN +1 = YFINAL ; YN = YINT; ( XN ,YN ) = (XINT,YINT) ; h = Step-Length YN+1 = YN + h ( XN,YN ) In the write up of this Matlab Assignment I will discuss every part of code in sequential order as it is used.

Requirements One and Two. Input

As you can see from the above text the code starts with clc this command clears everything that was in the command window. As I was writing the code my command window quickly became a mess with each time I performed the script and inbuilt functions. Therefore, I thought it would be a good idea to incorporate this feature at the start of every script. The next few lines of code prompt the User to input the different range of values needed to use Eulers Method. These values are then stored in Matlab memory as XINT, XFINAL, YINT, h. The next line displays default text asking the User to type the name of the relevant function they wish to use. USERFUNCTION then stores the Users selection in Matlabs Cache.

EULER The next line refers to Euler. Euler is the name of a function I wrote which uses all the variables enclosed in the brackets following it. I will now divert to the function Euler and explain what it does.

The first line of this function sol = feval(USERFUNCTION,XINT,YINT) evaluates the function handle, using the arguments enclosed within the brackets and stores the answer as sol. The next piece of code is the Loop which uses Eulers Method to calculate a final value for Y. The first statement of the loop means that the following code will keep repeating while the initial x value is less than the value the user gave for the final x value.

Input Reverting now back to the script we now have a value for the final value of Y. The last line of code in this script displays the words Final Point followed by the result on the next line.

Shown below is a sample equation from the book with an initial x value of 0, final x value of 1, initial y value of 0 and a step-length of 0.2. In the book it states that the final point for Y should be 0.4883.

F2 Next, I will use the following values to obtain an approximate solution for the second function F2. XINT = 1, XFINAL = 2, YINT = 0, h = 0.2

In order to prove this is the correct value I performed the calculation manually and reached the same approximation.

This shows us Eulers Method at its most basic, generating an approximate solution to the first and second function shown on the Matlab Assignment Sheet.

Requirements Three and Four.


The Matlab sheets state to perform all three methods i.e Euler and the two forms of the Runge-Kutta and display their different values to show the accuracies of all three methods.

Input2

The first parts of this code requests from the user the values they wish to use to solve the equation using Eulers Method. The code then prompts the user to press the keys 1 or 2 depending on which function they wish to use. If the user presses 1, ifask1 will be performed, if the user presses 2, ifask2 will be performed.

ifask1

Shown above is the function named ifask1. If the User selected 1 this function will be called. This function uses Matlabs inbuilt Runge-Kutta methods to determine the values using F1. F1 is another function I made that has the actual equation in it. The disp commands following this display the different values each method calculated.

F1

Shown above is the Function named F1. This is the first function shown on the assignment sheet. However, the user may change the equation to suit whatever function they wish to perform Eulers Method on.

Shown below is the function named ifask2. If the User selected 2 this function will be called. This function uses Matlabs inbuilt Runge-Kutta method to determine the values using F2. F2 is another function I made that has the actual equation in it and is shown below:

ifask2

This is the function that is called if the User pressed 2. Its output will be T1, Y1, T2, and Y2. It performs the Runge-Kutta methods on the values given using Function 2 on the assignment sheet. It then displays these results in a format that makes it easy for the User to differentiate between the different results. And hence being able to decide which is the more accurate method. F2

This is the second function shown on the assignment sheet. However, the user may format the equation as in F1 decreasing the limitations of this code.

Requirements 5
Q5

Shown here is a script that compares Eulers Method to the Runge-Kutta Method. Runge-Kutta is an inbuilt function in Matlab. ODE23 and ODE45 are used to implement it. This code again like the other requirements that went before asks the user for the various values needed to perform Eulers Method. The script then asks the user which function they would like to perform the Euler calculation on. As in previous scripts, depending on which button the user presses, decides which function will be used.

E1

If 1 was selected this function is called. Starting from the top, in order to tell the program how many steps there was between the initial X and Final X I asked it to calculate it by taking the final x value and subtracting the initial x value and dividing this answer by the step length. This value was then given the variable name step. I required the use of an integer i and gave it the value one, this was for the purpose of increasing i by 1 every time for the loop. In order to reduce the amount of typing I would have to do I renamed XINT as X and YINT as Y. I already explained the loop in the previous requirement. When this loop completed it enters the value into F1 and the Runge-Kutta method is performed. The following lines of code are the various modifications I made to the graph i.e. colour, font, legend, title, etc. I was able to find this code by editing the plot once the graph was called. I then made the modifications I wanted and asked the program to print a .m file of the modifications which I then put into my code.

Graph of F1 with Eulers and the Two Forms of RungeKutta.

Using the same values as before i.e XINT = 0, XFINAL = 1, YINT = 0, h = 0.2. As we can see from the graphs legend, Eulers Method does not give as accurate an approximation as both Runge-Kutta methods.

E2

This function compares Eulers Method to the Runge-Kutta Method using F2. Each input was received from the user in the script file. This function then manufactures a matrix using these results, which are then plotted on one graph as shown below.

Graph for Function Two F2.

The graph shows blue, green and red lines as shown in the legend. We can see that ode 23 and 45 are basically the same. While Eulers seems to veer away.

Below is all the code I used. clc XINT = input('Enter Initial Value for X: '); XFINAL = input('Enter Final Value for X: '); YINT = input('Enter Initial Value for Y: '); h = input('Enter Step-Length(h): '); disp('Please type the name of the function you wish to perform Eulers Method on:'); USERFUNCTION = input(' i.e F1 or F2: ','s'); YFINAL = Euler( XINT, XFINAL, YINT, h, USERFUNCTION ); disp('Final Point:');disp(YFINAL) function [ F2 ] = F2( XINT,YINT ) %F2 is the second function on the Matlab Assignment Sheet % As with F1 the User is able to format this equation as they see fit. F2 = (YINT + 3*(XINT))/(XINT^2); end function [ F1 ] = F1( XINT,YINT ) %F1 is the first function on the Matlab Assignment Sheet. % However the User may change this function to whatever they wish to % perform Eulers Method on. F1 = XINT + YINT; end function [ YFINAL ] = Euler( XINT, XFINAL, YINT, h, USERFUNCTION ) %Euler is a function which uses the Users input to determine which function %to perform Euler's Method on. sol = feval(USERFUNCTION,XINT,YINT); while XINT < XFINAL; YFINAL = YINT + h * sol; YINT = YFINAL;

XINT = XINT + h; sol = feval(USERFUNCTION,XINT,YINT); end clc XINT = input('Enter Initial Value for X: '); XFINAL = input('Enter Final Value for X: '); YINT = input('Enter Initial Value for Y: ');

disp('Which code would you like to use ?'); disp(' i.e To use the function dy/dx = X + Y press 1,'); ask = input(' Or to use the function dy/dx= Y+3X/X^2 press 2: '); if ask == 1 ifask1(XINT,XFINAL,YINT)

elseif ask == 2 ifask2(XINT,XFINAL,YINT)

end function [ T1,Y1,T2,Y2 ] = ifask2( XINT,XFINAL,YINT ) %ifask2 is the function that is called depending on the Users selection %from Input2

[T1,Y1] = ode23('F2',[XINT,XFINAL],YINT); [T2,Y2] = ode45('F2',[XINT,XFINAL],YINT); disp('ode23'); disp([T1 Y1]); disp('ode45'); disp([T2 Y2]) %plot(T1,Y1,T2,Y2) end function [ T1,Y1,T2,Y2 ] = ifask1( XINT,XFINAL,YINT )

%ifask1 is the function that is called depending on the Users selection %from Input2 [T1,Y1] = ode23('F1',[XINT,XFINAL],YINT); [T2,Y2] = ode45('F1',[XINT,XFINAL],YINT); disp('ode23'); disp([T1 Y1]) disp('ode45'); disp([T2 Y2]) end clc XINT = input('Enter Initial Value for X: '); XFINAL = input('Enter Final Value for X: '); YINT = input('Enter Initial Value for Y: '); h = input('Enter Step-Length(h): '); disp(' i.e To use the function dy/dx = X + Y press 1,'); type = input(' Or to use the function dy/dx= Y+3X/X^2 press 2: ');

if type == 1 E1( XINT,XFINAL,YINT,h )

elseif type == 2 E2( XINT,XFINAL,YINT,h ) end

function [ b,a ] = E1( XINT,XFINAL,YINT,h ) %E1 is the function called if the user wishes to perform Eulers Method on %the first equation on the Assignment Sheet. % This function calls F1, and Eulers Method and the two forms of % Runge-Kutta are calculated and a graph is plotted displaying the % accuracies of these methods. step = (XFINAL - XINT)/h; i = 1;

a = 1:step; b = 1:step; X = XINT; Y = YINT; while X <= XFINAL; b(i)= Y; YFINAL = Y + (h * (X + Y)); Y = YFINAL; a(i) = X; X = X + h; i = i + 1; end [T1,Y1] = ode23('F1',[XINT,XFINAL],YINT); [T2,Y2] = ode45('F1',[XINT,XFINAL],YINT); b = b'; a = a'; disp('ode23'); disp(Y1); disp('ode45'); disp(Y2); figure1 = figure('Color',[0 .7 0]); axes1 = axes('Parent',figure1,'Color',[0.862745098039216 0.862745098039216 0.862745098039216]); box(axes1,'on'); hold(axes1,'all'); xlabel('X Values','FontWeight','bold','FontSize',12,... 'FontName','Arial'); ylabel('Y Values showing Initial Y Value','FontWeight','bold','FontSize',12,... 'FontName','Arial'); title('\it{Eulers Method compared with two Forms of Runge Kutta}',... 'FontSize',16,'Color',[1 1 0]); plot(T1,Y1,T2,Y2,a,b);

map = legend('Runge-Kutta ODE23','Runge-Kutta ODE45','Eulers Method'); set(map,'Location','Northwest'); disp('Eulers Method'); end

function [ Yn ] = E2( XINT,XFINAL,YINT,h ) %E2 is the function called if the user wishes to perform Eulers Method on %the second equation on the Assignment Sheet. % This function calls F2, and Eulers Method and the two forms of % Runge-Kutta are calculated and a graph is plotted displaying the % accuracies of these methods. step = (XFINAL - XINT)/h; i = [1:step]; x = XINT; y = YINT; while x <= XFINAL; b(i) = y; Yn = y + (h * (y + 3*x)/(x^2)); y = Yn; a(i) = x; x = x + h; i = i + 1; end [T1,Y1] = ode23('F2',[XINT,XFINAL],YINT); [T2,Y2] = ode45('F2',[XINT,XFINAL],YINT); disp('ode23'); disp(Y1); disp('ode45'); disp(Y2); figure1 = figure('Color',[0 .7 0]); axes1 = axes('Parent',figure1,'Color',[0.862745098039216 0.862745098039216 0.862745098039216]); box(axes1,'on');

hold(axes1,'all'); xlabel('X Values','FontWeight','bold','FontSize',12,... 'FontName','Arial'); ylabel('Y Values showing Initial Y Value','FontWeight','bold','FontSize',12,... 'FontName','Arial'); title('\it{Eulers Method compared with two Forms of Runge Kutta}',... 'FontSize',16,'Color',[1 1 0]); plot(T1,Y1,T2,Y2,a,b); map = legend('Runge-Kutta ODE23','Runge-Kutta ODE45','Eulers Method'); set(map,'Location','Northwest'); disp('Eulers Method'); end

You might also like