0% found this document useful (0 votes)
18 views13 pages

Mini Project Cal 2

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

Mini Project Cal 2

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

The University of Danang-University of Science and Technology

Faculty of Advanced Science and Technology




Mini-Projects for Calculus 2

Lecture: Phạm Quý Mười


Class:22ECE
Member :
- Đỗ Lâm Khánh
- Trần Thanh Hải
- Bùi Hoàng Nguyên
- Đinh Ngọc Thiện

Saturday , December 9th 2023


1
= ASSIGNMENT =

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.

 Members' working process

Member Finished Note


Trần Thanh Hải  Content moderation
Đỗ Lâm Khánh  Do Word
Bùi Hoàng Nguyên 
Đinh Ngọc Thiện 

2
Ⅰ. Problem 1
1. Program to draw slope field of y’=f(t,y).

% Define the differential equation y' = f(t, y)


f = @(t, y) t - y; % You can change the equation here

% Define the range for t and y


t_range = linspace(-5, 5, 20);
y_range = linspace(-5, 5, 20);

% Create a meshgrid for t and y


[T, Y] = meshgrid(t_range, y_range);

% Calculate the slope at each point


dy_dt = f(T, Y);

% Normalize the vectors to make them visually consistent


dy_dt_normalized = dy_dt ./ sqrt(1 + dy_dt.^2);

% Plot the slope field using quiver


figure;
quiver(T, Y, ones(size(dy_dt_normalized)), dy_dt_normalized, 0.5, 'r');
title('Slope Field for y'' = t - y'); % You can change the equation here
xlabel('t');
ylabel('y');

% Adjust plot settings if necessary


axis tight;
grid on;

3
2. Illustrate the solutions of some IVP.
a) y′=−2ty with the initial condition y(0)=1

% Define the differential equation y' = -2ty


f = @(t, y) -2 * t * y;

% Initial condition
y0 = 1;

% Time span
tspan = [0, 2];

% Solve the IVP using ode45


[t, y] = ode45(f, tspan, y0);

% Plot the solution


figure;
plot(t, y, 'o-');
title('Solution of y'' = -2ty with y(0) = 1');
xlabel('t');
ylabel('y');
grid on;

4
b) y′=−0.5y+2 with the initial condition y(0)=1.

% Define the differential equation y' = -0.5y + 2


f = @(t, y) -0.5 * y + 2;

% Initial condition
y0 = 1;

% Time span
tspan = [0, 5];

% Solve the IVP using ode45


[t, y] = ode45(f, tspan, y0);

% Plot the solution


figure;
plot(t, y, 'o-');
title('Solution of y'' = -0.5y + 2 with y(0) = 1');
xlabel('t');
ylabel('y');
grid on;

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 .

Figure 1 Graphical interpretation of the first step of Euler’s method.

So the slope at x  x0 as shown in Figure 1 is


Rise
Slope 
Run
y  y0
 1
x1  x0
 f x0 , y 0 
From here
y1  y 0  f x0 , y 0 x1  x0 
Calling x1  x0 the step size h , we get
y1  y 0  f x0 , y 0 h
One can now use the value of y1 (an approximate value of y at x  x1 ) to
calculate y 2 , and that would be the predicted value at x2 , given by
y 2  y1  f x1 , y1 h
x 2  x1  h

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.

Figure 2 General graphical interpretation of Euler’s method.

2. Programing codes ( C++)

#include<bits/stdc++.h>

// Function to represent the first-order ODE y' = func(y, t)


double differentialEquation1(double y, double t,double a, double b,double
c) {
// You can modify this function based on your specific ODE
return a*t+b*y+c; } // Example: y' = ax+by+c
double differentialEquation2(double y, double t,double a, double b,double
c,double d, double e) {
// You can modify this function based on your specific ODE
return a*pow(t,2)+b*pow(y,2)+c*t+d*y+e; // Example: y' =
ax^2+by^2+cx+dy+e
}

// Euler's method for solving a first-order ODE


void eulerMethod1(double (*func)(double, double, double, double, double),
std::vector<double>& tValues, std::vector<double>& yValues, double y0,
double t0, double tn, double h,double a, double b, double c) {
int numSteps = static_cast<int>((tn - t0)*1000 / (h*1000));
//std::cout<<" "<<tn<<" "<<t0<<" "<<h<<"
"<<numSteps<<std::endl;
//int numSteps = ((tn - t0) / h);
tValues.resize(numSteps + 1);
7
yValues.resize(numSteps + 1);
tValues[0] = t0;
yValues[0] = y0;
//std::cout<<" "<<numSteps<<std::endl;
for (int i = 0; i < numSteps; ++i) {
tValues[i + 1] = tValues[i] + h;
yValues[i + 1] = yValues[i] + h * func(yValues[i],
tValues[i],a,b,c);
// if (numSteps==2)
//std::cout<<" "<<tValues[i+1]<<"
"<<yValues[i+1]<<std::endl;
}
}
void eulerMethod2(double (*funct)(double, double, double, double, double,
double, double), std::vector<double>& tValues, std::vector<double>&
yValues, double y0, double t0, double tn, double h,double a, double b,
double c,double d,double e) {
int numSteps = static_cast<int>((tn - t0)*1000 / (h*1000));
tValues.resize(numSteps + 1);
yValues.resize(numSteps + 1);
tValues[0] = t0;
yValues[0] = y0;

for (int i = 0; i < numSteps; i++) {


tValues[i + 1] = tValues[i] + h;
yValues[i + 1] = yValues[i] + h * funct(yValues[i],
tValues[i],a,b,c,d,e);
}
}

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;

std::cout << "Enter initial time x0: ";


std::cin >> t0;

std::cout << "Enter final time xn(xmax): ";


std::cin >> tn;

std::cout << "Enter step size h: ";


std::cin >> h;

// Solve the ODE using Euler's method


//std::vector<double> t, y;

if (select ==1) eulerMethod1(differentialEquation1, t, y, y0, t0, tn,


h,a,b,c);
else
eulerMethod2(differentialEquation2, t, y, y0, t0, tn, h,a,b,c,d,e);

// Print the results


for (size_t i = 1; i < t.size(); ++i) {
std::cout << "t = " << std::fixed << std::setprecision(2)<<t[i]
<< ", y = " << std::fixed << std::setprecision(2)<< y[i] << std::endl;
}

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:

Continuing this process,

we get a sequence of t and y values.

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:

Continuing this process, we get a sequence of t and y values.

11
- Solution 2 :
Using Euler's method with a step size ℎ=0.1, starting from t=0 and y=0.5:

Continuing this process, we get a sequence of t and y values.

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.

Transparency: The method offers


transparency in terms of
understanding each step in the
approximation process. This can be
beneficial for educational purposes
and for gaining an intuitive
understanding of numerical
methods.

13

You might also like