Euler Method without using ODE solvers

I am trying to write a code that will solve a first order differential equation using Euler's method(Improved Euler's, Modified Euler's, and Euler-Cauchy). I don't want to use an ode solver, rather would like to use numerical methods which will return values for (x,y) and f(x,y) and plot of function f. I'm not sure how to begin to write this in MATLAB. I have solved the equation by hand and I'm now trying to write a code that solves that equation. Formulas for Improved Euler's, Modified Euler's, and Euler-Cauchy which I need to use are:
Improved Euler's:
Modified Euler's:
Euler-Cauchy's:
The equation to be used is y'=xy^2+y, y(0)=1, x∈[0,0.5], with step h=0.05
Any help is appreciated. Thank you in advance.

답변 (1개)

James Tursa
James Tursa 2015년 6월 17일
편집: James Tursa 2015년 6월 17일

1 개 추천

What have you done so far? Can you code up the simple Euler's Method? E.g., if you started with this generic expression of Euler's Method:
y(i+1) = y(i) + h * f(x(i),y(i))
can you put that inside of a MATLAB m-code for loop to generate results for some range of x with a given h?
Then, using that code, modify it for the other methods you are interested in.
Your "Modified Euler's" method doesn't look like Modified Euler to me ... it just looks like Euler's method using a h/2 step instead of a h step. (Maybe there is more to these methods in your doc that you didn't post?). E.g., see this link:
https://en.wikipedia.org/wiki/Heun%27s_method

댓글 수: 6

Marko Kovacevic
Marko Kovacevic 2015년 6월 17일
I'm begginer, this is my first touch with Matlab, so I have no idea how to even put a code in it. I only have some knowledge in Java. I've found some help on google but I couldn't find out where they use t, %x etc (for example this: http://www.math.ku.edu/~mandal/math320/euler.html)
James Tursa
James Tursa 2015년 6월 17일
편집: James Tursa 2015년 6월 17일
OK, here is a simple outline to get you started:
% Euler's Method
% Initial conditions and setup
h = 0.05; % step size
x = 0:h:0.5; % the range of x
y = zeros(size(x)); % allocate the result y
y(1) = 1; % the initial y value
n = numel(y); % the number of y values
% The loop to solve the DE
for i=1:n-1
f = the expression for y' in your DE
y(i+1) = y(i) + h * f;
end
You need to figure out how to write the code for f and insert it into the place I have indicated. Then you will have Euler's Method completed.
For your other methods, you will need to expand the code to calculate the multiple f values and y values and then combine them via the formula that is given.
(I used the variable i in the indexing above so that the code looks like the formulas in your text. Normally I would have used k for an index since MATLAB uses i for the imaginary value sqrt(-1), but I didn't want to confuse you with that since k has a specific meaning in some of the Runge Kutta links you may read)
Marko Kovacevic
Marko Kovacevic 2015년 6월 21일
편집: Marko Kovacevic 2015년 6월 21일
I've searhed tons of resources on the Internet, but I didn't found how to implementate the code for f which will give me final Euler's Method. Can anyone give me more informations on how to do that?
when I enter: f = x * y.^2 + y; It says: Error using * Inner matrix dimensions must agree.
James Tursa
James Tursa 2015년 6월 22일
You need to index into the x and y variables for the specific iteration you are doing. I.e., use x(i) and y(i), not x and y.
Sanjida Ahmed
Sanjida Ahmed 2016년 4월 11일
Running this code,it gives nothing to display.whats the problem?I am new in Matlab.please help me.
James Tursa
James Tursa 2016년 4월 11일
What code are you running? If you have a different problem to solve, it would be better to open up a new question.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

질문:

2015년 6월 17일

댓글:

2016년 4월 11일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by