I have to write application which solves task presented below. I only know some c# so I will stick to it. It is some kind of homework but I am asking for help with understanding this and advice for implementation. Take natural n>=1 and real a>1 differential problem y′(x)=f(x,y(x)),y(1)=0 where f(x,y)=sqrt(y+1)+x solve for [1,a] with step h=a−1/n with Euler and Heun methods. Compare results with exact solution y(x)=x^2−1 by finding maximum error of each method.

kaltEvallwsr

kaltEvallwsr

Answered question

2022-11-13

I have to write application which solves task presented below. I only know some c# so I will stick to it. It is some kind of homework but I am asking for help with understanding this and advice for implementation.

Take natural   n >= 1 and real   a > 1
differential problem
  y ( x ) = f ( x , y ( x ) ) , y ( 1 ) = 0
where   f ( x , y ) = y + 1 + x
solve for   [ 1 , a ] with step   h = a 1 n with Euler and Heun methods.

Compare results with exact solution   y ( x ) = x 2 1 by finding maximum error of each method.

Answer & Explanation

hocelwsmjc

hocelwsmjc

Beginner2022-11-14Added 16 answers

You are being asked to write a program that allows a user to select between the Euler and Heun (Improved Euler) Method and allows the user to input two parameters, n and a.

These two parameters affect the output of the program and the results of the calculations.

The Euler Method is given by:
y = f ( x , y ) = y + 1 + x , 1 x a , y ( 1 ) = 0

The algorithm to code up is:
Input a
Input n
h = a 1 n
x 1 = 1
y 0 = 0
For i = 1 , 2 , n
              y i = y i 1 + h f ( x i 1 , y i 1 )
              x i = 1 + i h

For example:
a = 2 , n = 10 h = 0.1 , x 0 = 1 , y 0 = 0

This yields:
y 1 = y 0 + h f ( x 0 , y 0 ) = 0 + 0.1 ( y 0 + 1 + x 0 ) = 0.2
x 1 = 1 + 1 × 0.1 = 1.1
y 2 = y 1 + h f ( x 1 , y 1 ) = 0.419545

y 10 = 2.884512

When you compare this to the actual result, you get:
| y E x a c t ( 2 ) y E u l e r ( 2 ) | = | ( 2 2 1 ) 2.884512 | = 0.115488.
For the Heun Method, you need only change the iteration formula to:
y i = y i 1 + h 4 ( f ( x i 1 , y i 1 ) + 3 f ( x i 1 + 2 3 h ,     y i 1 + 2 3 h f ( x i 1 , y i 1 ) ) )

Repeating the same example as above, yields:
y 1 = 0.209772

y 10 = 2.998037
Error = | 3 2.998037 | = 0.001963 (a much better result).

Of course, you can play around with a and n once you have it coded up and see the impacts of changing those two parameters on the results, which is intended to teach you about numerical algorithms and errors with them.

Lastly, you need to work the maximum error based on all of the given information and the maximum error of each algorithm and the parameters a and n, but I'll leave that to you.

Do you have a similar question?

Recalculate according to your conditions!

Ask your question.
Get an expert answer.

Let our experts help you. Answer in as fast as 15 minutes.

Didn't find what you were looking for?