0% found this document useful (0 votes)
64 views10 pages

Application of Euler's Method Continuation - PROGRAMMING CODES

The document provides an algorithm for applying the Euler's method to approximate solutions of differential equations. It outlines 10 steps for the algorithm, including defining initial values, the step size, the given function, calculating derivatives, and using a loop to iteratively calculate next y-values according to the Euler's method formula until reaching the given endpoint condition. It then translates this algorithm into a QBASIC pseudocode program by specifying how each step would be implemented as a QBASIC statement or series of statements.

Uploaded by

glockenspiel9971
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views10 pages

Application of Euler's Method Continuation - PROGRAMMING CODES

The document provides an algorithm for applying the Euler's method to approximate solutions of differential equations. It outlines 10 steps for the algorithm, including defining initial values, the step size, the given function, calculating derivatives, and using a loop to iteratively calculate next y-values according to the Euler's method formula until reaching the given endpoint condition. It then translates this algorithm into a QBASIC pseudocode program by specifying how each step would be implemented as a QBASIC statement or series of statements.

Uploaded by

glockenspiel9971
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

THE ALGORITHM on how we apply the Euler‟s Method on the above example (...

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):

Order of (our) process:

step action

1. Define the item number


2. Define the initial value for x
3. Define the initial value for y
4. Define the step size
5. Define the given function
6. Define the given x condition given as y(x), in which you stop the approximate solution: at the given x
here.
7. Define the first derivative of the function (which is the slope of the line from the initial point to the next
point in question)
𝑑𝑦
8. Define the next value of y (y(n+1) or y(x+h), same thing) by substituting the values of (x+h), =
𝑑𝑥
𝑓(𝑥, 𝑦), and h into the Euler's Method
9. Display (or print on screen) the output.
10. Repeat step 8 to step 9 to get the next value of y, but use the previous values of y and x to solve for the
next value of y until you reach the value of y(x) given condition.

May QBASIC pa kamo nga


galamiton? Sa PC nyo?
WALA na gid, Sir…

Hay may EXCEL


pa man bala?
…medyo…
Let‟s construct the QBASIC pseudocode, by translating each step (from the algorithm) to a QBASIC statement
(command).

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

7. Create variable for the first derivative of the given function,


dydx „variable for the first derivative of fx

and then

Calculate the slope for the next point


dydx = 2*x

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

print “ n”, “ x(n)”, “ y(n)”, “f(x)”

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.

„initialize the given values

n = 0
x0 = 1
y0 = 1
h = 0.1

fx = x^2
xn = 1.5

„first, display the initial values on screen


„with tabulated formatting

PRINT “=======================================================”
PRINT “ n”, “ x(n)”, “ y(n)”, “f(x)”
PRINT “=======================================================”
PRINT n, x0, y0, x0^2

„start of the loop

FOR x = x0 TO xn STEP h

dydx = 2 * x

y = y + (dydx * h)

fx = (x + h)^2

n = n + 1

PRINT “ n”, “ x(n)”, “ y(n)”, “f(x)”

NEXT x

END
Save the pseudocode (in QBASIC), and press F5 to run the program.

The output will be:

Let‟s evaluate the code…

„initialize the given values

n = 0 ~ this is the item counter, n


x0 = 1 ~ the values of the x-not, and y-not, (the initial condition); can also be expressed
y0 = 1 as y(1) = 1, in this example
h = 0.1 ~ this is the step size

fx = x^2 ~ the given function, y=x2


xn = 1.5 ~ the condition at which we have to estimate the value of y, given as y(1.5)
For the screen output, the
format is 4 columns, headed
„first, display the initial values on screen by n, x(n), y(n), and f(x). The
„with tabulated formatting COMMA after each “string”
forces the headers to be
PRINT “=======================================================”
printed by print zones.
PRINT “ n”, “ x(n)”, “ y(n)”, “f(x)”
PRINT “=======================================================”
PRINT n, x0, y0, x0^2

The actual (calculated / assigned) values of the variables


n, x0, y0, and x0^2 are printed after the strings of
“=====”
„start of the loop
This loop will start with x = 1, and ends until x = 1.5, while incrementing the x value by 0.1
So you see, we have 6 loops in all:
first loop: x = 1, before the second loop, add 0.1 to 1
second loop: x = 1.1, before the third loop, add 0.1 to 1.1
third loop: x = 1.2, before the fourth loop, add 0.1 to 1.2
fourth loop: x = 1.3, before the fifth loop, add 0.1 to 1.3
fifth loop: x = 1.4, before the sixth loop, add 0.1 to 1.4
sixth loop: x = 1.5, end of the LOOP

(then the looping exits when x = xn)

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:

y = y + (dydx * h) Notice this line: if you can still remember,


this value assignment to the variable y (the leftmost „y‟)
is called the ACCUMULATOR.
The „y‟ on the left of the equality sign is the NEXT VALUE of y
The „y‟ after the equality sign is the CURRENT VALUE of y

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 outputs the value on the screen:

PRINT n, x+h, x0+y,fx

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)

B1 = 0.1 ~ this is the step size


B5 = 1 ~ this is initial value for x
C5 = 1 ~ this is initial value for y

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

B6 = B5 + $B$1 ~ x0+ h, and subsequent x+ h

Now, select the cell (B6), click COPY (or CTRL+C), select the cells (B7 to B10), click PASTE
(or CTRL+V)

C6 = C5 + (D5 * $B$1) ~ this is the actual Euler's Method (formula)

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.

GENERATE A GRAPH IN EXCEL

Take note that the dydx column was HIDDEN – you right click on the D column header, and select HIDE.

Select the cells from B5 to E10.

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.

ENGINEERING at its best!!!


NOW, if you want to have a very “near” approximations of values using Euler's Method, we can do so by decreasing the
step size, to produce a smaller “increments” from previous x to the next x. That is, the smaller the step, the more accurate
the approximations.

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.

If h = 0.05: we have 10 points


If h = 0.01: we have 50 points

“hindi kasya sa screen sa


isang “RUN” session”

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)

MASADYA ANG NUMERICAL METHODS…

Hay pwede nyo „ni


maobrahan code sa
MATLAB?

…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!

You might also like