Using the Script:
function EulerMethod1(n)
X = 0 : 1/n : 1 ;
Y = zeros( 1, n + 1 ) ;
Y(1) = 1 ;
for k = 1 : n
m = Y(k) ;
Y(k + 1) = Y(k) + m*( X(k + 1) - X(k) ) ;
end
clf
plot( X, Y )
Create a new script, which defines the function EulerMethod2. The purpose of EulerMethod2 is to use Euler's Method to approximate the solution to the Initial Value Problem given below.
dy/dx = 2xy where. y (0) = 1. over the interval. [ 0, 2 ]
Required Matlab code with explanatory comments where changes were made is given below:
function EulerMethod2(n)
X = 0 : 1/n : 2 ; %changed to 0:1/n:2 as we need solution over [0 2]
Y = zeros( size(X) ) ; %change so Y and X have same sign
Y(1) = 1 ;
for k = 1 : length(X)-1
m = 2*X(k)*Y(k) ; %changed to y'=f(x,y)=x*y
Y(k + 1) = Y(k) + m*( X(k + 1) - X(k) ) ;
end
clf
plot( X, Y )
Resulting plot:
Hope this was helpful. Please do leave a positive rating if you liked this answer. Thanks and have a good day!
Get Answers For Free
Most questions answered within 1 hours.