Application of Euler's Method Continuation - PROGRAMMING CODES
Application of Euler's Method Continuation - PROGRAMMING CODES
y=x2)
This is as simple as WRITING DOWN the whole process in detailed, “step-by-step” manner, from START to FINISH, as
what we have done just recently.
The first step is…? Well, we can just start the process from page 7, but we can actually begin from page 9 (sounds
confusing again!)
From page 7 (of the previous PDF file), we have listed all the given information we NEED to find the approximate
solution using Euler's Method.
Here‟s how we construct the algorithm based on the procedure (so far):
step action
step action
1. Create variable for the item subscript (n) and initialize it to zero
n = 0
2. Create variable for the initial value of x, and assign it the given initial value
x0 (variable)
x0 = 1
3. Create variable for the initial value of y, and assign it the given initial value
y0 (variable)
y0 = 1
4. Create variable for the step size, and assign it the given value
h (variable)
h = 0.1
5. Create variables for the given function, and assign it the actual value
fx „variable for the function
fx = x^2
6. Create variable for y(x) condition, and assign its given x-value
xn (variable)
xn = 1.5
and then
8. Create variable defining the next value of y, and assign it the Euler's Method format (translate it
syntactically: PEMDAS)
y (variable)
y = y + (dydx*h)
9. Display the values on the screen, by creating a “tabular” formatting for the output
10. Create a LOOP that will repeat step 7 (at the and then clause) to step 9 until the estimated value for y(x) =
1.5 is reached. We will use the FOR…NEXT statement to create our loop.
n = 0
x0 = 1
y0 = 1
h = 0.1
fx = x^2
xn = 1.5
PRINT “=======================================================”
PRINT “ n”, “ x(n)”, “ y(n)”, “f(x)”
PRINT “=======================================================”
PRINT n, x0, y0, x0^2
FOR x = x0 TO xn STEP h
dydx = 2 * x
y = y + (dydx * h)
fx = (x + h)^2
n = n + 1
NEXT x
END
Save the pseudocode (in QBASIC), and press F5 to run the program.
FOR x = x0 TO xn STEP h
For every loop, the value of dydx is calculated (as 2 times x), and assigns the product on the dydx variable:
dydx = 2 * x
then the new value of y is calculated here, using the value in dydx and h:
Therefore, at the start first loop (only), the CURRENT value of y is…ZERO, because we have not
assigned a value to y at the beginning of the code. (or in other words, the variable y was NOT
INITIALIZED). Now after this line has been executed, the value assigned to y (in the memory) is the
product of dydx and h
This line will calculate the ACTUAL (or analytical) value of the function f(x):
fx = (x + h)^2
This line increments the value of n, indicating the next item number for our tabulated output (only):
n = n + 1
This line increments the value of x (by h), and begins the next loop, until x reaches the value 1.5
NEXT x
END
In MS Excel, we would have this output:
The value assignments are: (just type the values directly in the cells, ignore the equality sign)
Just type the values 0 to 5 in cells A5 to A10 (just type the values directly in the cells, ignore the equality sign)
The formula: You can enter a formula on a cell by selecting the cell, typing the equal sign (=) followed by the
expression
cell formula
Now, select the cell (B6), click COPY (or CTRL+C), select the cells (B7 to B10), click PASTE
(or CTRL+V)
Now, select the cell (C6), click COPY, select the cells (C7 to C10), click PASTE
NOTE: I included the values of dydx for clarity, though it not really necessary
D5 = 2*B5
Now, select the cell (D5), click COPY, select the cells (D6 to D10), click PASTE
E5 = B5^2
Now, select the cell (E5), click COPY, select the cells (E6 to E10), click PASTE
I managed to create the graph of the actual curve, against the approximated curve in excel:
The red line (indicated as Series3 in the legend) is the EXACT curve/solution (the actual curve of the function y = x2), and
each point (xn, fx) is indicated as a white dot.
The blue line (indicated as Series1 in the legend) is the APPROXIMATE curve/solution (calculated using the Euler's
Method), and each estimated point (xn, yn) is indicated as a blue triangle.
Take note that the dydx column was HIDDEN – you right click on the D column header, and select HIDE.
On the ribbon, click INSERT, select SCATTER, and click SCATTER WITH SMOOTH LINES AND MARKERS.
That‟s it.
I have formatted the graph above so that the lines and markers are smaller and clearer.
If, on the QBASIC pseudocode, we decrease the step size from 0.1 to, say, 0.05, then we have a smaller interval between
x-values. The result is that we have more points to plot. Just change the value of h in the code.
Imagine if we have to
calculate these points
manually…one by one…
If you plot these points, we can have a “more” accurate approximate solution for the given function (y = x 2)
…ayabaw…
FIGHT!
You can always refer to YouTube video demonstrations about Euler's Method and programming on MATLAB. Budget
naman para sa internet connection…
This is the high time you will appreciate programming as an indispensable tool in engineering numerical applications.
Sana, whole!