Mini Project Cal 2
Mini Project Cal 2
Member Content
Trần Thanh Hải ( leader ) Write a program to draw slope field of
y’=f(t,y).
Đỗ Lâm Khánh Write a program for problem 2 and do 2
examples
Bùi Hoàng Nguyên Problem 2 : summarize the theory and
computational formulas , shows the
advantages and disadvantages of the
methods.
Đinh Ngọc Thiện Problem 1 : Illustrate the solutions of
some IVP.
2
Ⅰ. Problem 1
1. Program to draw slope field of y’=f(t,y).
3
2. Illustrate the solutions of some IVP.
a) y′=−2ty with the initial condition y(0)=1
% Initial condition
y0 = 1;
% Time span
tspan = [0, 2];
4
b) y′=−0.5y+2 with the initial condition y(0)=1.
% Initial condition
y0 = 1;
% Time span
tspan = [0, 5];
5
Ⅱ. Problem 2
1. Euler’s method for the first order equations: theory and computational
formulas
a) Definition
- Euler's method is a numerical technique for solving ordinary differential equations
(ODEs) with a given initial value. It is a first-order method and is relatively simple
to implement. The basic idea is to approximate the solution by taking small steps
along the direction of the derivative at each point.
b) Computational formulas
- At x 0 , we are given the value of y y 0 . Let us call x 0 as x0 . Now since we
know the slope of y with respect to x , that is, f x, y , then at x x0 , the slope is
f x0 , y 0 . Both x0 and y 0 are known from the initial condition y x0 y 0 .
6
Based on the above equations, if we now know the value of y y i at xi , then
y i 1 y i f xi , yi h
This formula is known as Euler’s method and is illustrated graphically in Figure
2. In some books, it is also called the Euler-Cauchy method.
#include<bits/stdc++.h>
int main() {
double y0, t0, tn, h,a,b,c,d,e,select;
std::vector<double> t, y;
std::cout<<"What polynomial equation?Choose from 1-2: "<<std::endl;
std::cin>>select;
if (select==1)
{
// Get user input for initial conditions and parameters
std::cout<<"---- y'=Ay+Bx+C -----------"<<std::endl;
std::cout<<"Enter the parameters of a ";
std::cin>>a;
std::cout<<"Enter the parameters of b ";
std::cin>>b;
std::cout<<"Enter the parameters: ";
std::cin>>c;
} else
{
std::cout<<"---- y'=Ay^2+Bx^2+Cy+Dx+E -----------"<<std::endl;
std::cout<<"Enter the parameters of a: ";
std::cin>>a;
std::cout<<"Enter the parameters of b: ";
std::cin>>b;
std::cout<<"Enter the parameters of c: ";
8
std::cin>>c;
std::cout<<"Enter the parameters of d: ";
std::cin>>d;
std::cout<<"Enter the parameters of e: ";
std::cin>>e;
}
std::cout << "Enter initial value y(0): ";
std::cin >> y0;
return 0;
}
3. Examples
a) Example 1 : y′=−0.1y
- Solution 1 :
Using Euler's method with a step size h=1, starting from t=0 and y=1:
9
Continuing this process, we get a sequence of t and y values.
- Solution 2
Using Euler's method with a step size h=0.1, starting from t=0 and y=1:
10
b) Example 2 : y′=2t−y
- Solution 1 :
Using Euler's method with a step size ℎ=0.5, starting from t=0 and y=0.5:
11
- Solution 2 :
Using Euler's method with a step size ℎ=0.1, starting from t=0 and y=0.5:
c) Conclusion
Euler’s Method
Advantages Disadvantages
Simplicity: Euler's method is Step Size Sensitivity: The method
straightforward and easy to is sensitive to the choice of the step
implement. It provides a simple size (h). If the step size is too large,
algorithm for approximating the method may diverge or
solutions to ODEs, making it introduce significant errors.
accessible for introductory Conversely, if the step size is too
12
numerical analysis. small, the method may be
computationally expensive.
Computational Efficiency: For
certain types of problems and when Not Self-Starting: Euler's method
computational resources are is not self-starting, meaning it
limited, Euler's method can be requires an initial guess or
computationally more efficient than condition to begin the iterative
more sophisticated methods. It process
requires basic arithmetic operations
and minimal computational
overhead.
13