Hi ,

I would like to know if someone can help me with aMatlab codeabout solving a differential equation using Heun's method.

I have been stuck with it for few days now and I need some help.

Please let me kow.

Braddy

Printable View

- Nov 23rd 2005, 11:12 AMbraddyinfo help for matlab/ODE
Hi ,

I would like to know if someone can help me with a__Matlab code__about solving a differential equation using Heun's method.

I have been stuck with it for few days now and I need some help.

Please let me kow.

Braddy - Nov 23rd 2005, 12:31 PMCaptainBlackQuote:

Originally Posted by**braddy**

problem.

RonL - Nov 23rd 2005, 02:15 PMbraddyQuote:

Originally Posted by**CaptainBlack**

Define a general function**heunstep(f,tcurr,tnext,xcurr,par)**that will take one step of Heun's method to solve a system of first order initial value problems. f is the handle to a function for evaluating the first derivatives.

**tcurr**is the current value of the independent variable,**tnext**is the value of the independent variable at which the solution is sought,**xcurr**is a column vector containing the current values of the dependent variables, and**par**is a vector containing any additional parameters required in the evaluation of f. ( for example if x'=rx1+mx2+kx1^3, vector**par**would be par=[r,m,k])

heunstep should return a column vector containing the values of the dependent variables at tnext. Design heunstep so that if par is the empty matrix, f will be evaluated at t and x by feval(f,t,x) while if par is not empty, f is evaluated at t and x by feval(f,t,x,par).

Thank you

B - Nov 23rd 2005, 11:56 PMCaptainBlack
The following function should do this. But note its untested as

I am at home at present and so don't have access to a machine

with Matlab.

The comments contain a link to a PDF file which provides an

explanation of the theory behind the method (as if it wasn't

obvious).

RonL

================================================== =

function RV=heunstep(f,tcurr,tnext,xcurr,par)

%

% function to take one step of Heun's method

% for integrating an ODE initial value problem

%

% Arguments: f-function handle for a function to evaluate the

% derivatives

% tcurr-current time

% tnext-time to step to

% xcurr-solution at tcurr

% par-row vector containing additional parameters

% for the derivative function

%

% Return Value: RV col vector xnext, xnext estimated solution at tnext

% xnext has the same dimensions as xcurr, and this should

% also be the same as the return value of the function that

% f is a handle for.

%

% reference:

%http://math.fullerton.edu/mathews/n2003/heunsmethod/Heun'sMethodProof.pdf

%

% Adapted from the code for: "Numerical Methods Using MATLAB", by John Mathews from Matlab Central.

%

% by CB 2005

%

h=tnext-tcurr;

k1=feval(f,tcurr,xcurr,par);

k2=feval(f,tnext,xcurr+h*k1,par);

xnext=xcurr+(h/2)*(k1+k2);

RV=xnext; - Nov 24th 2005, 01:45 AMCaptainBlack
You will still gave to modify or check that the code given satisfies these requirements:

Quote:

heunstep should return a column vector containing the values of the dependent variables at tnext. Design heunstep so that if par is the empty matrix, f will be evaluated at t and x by feval(f,t,x) while if par is not empty, f is evaluated at t and x by feval(f,t,x,par).

clone, and it appears to work.

RonL - Nov 24th 2005, 05:22 AMhpeQuote:

Originally Posted by**CaptainBlack**

- Nov 24th 2005, 05:25 AMbraddyQuote:

Originally Posted by**CaptainBlack**

I will be right back after testing it.

It seems to be ok.

B - Nov 25th 2005, 07:22 AMbraddyQuote:

Originally Posted by**braddy**

Now, I need to generate the real function heun ( with**n**steps) using heunstep ( you helped me with) as subprogram. I am gonna try to do it.

Again thank you very much.

B - Nov 26th 2005, 09:25 PMbraddyhelpQuote:

Originally Posted by**braddy**

I also have problem to understand how to present the solution(T and X)

I sent The all problem is on the attachment TASK.doc so you can see precisely what it is asked.

( if you want I can send you what I tried but as I said it does not work)

Thank you very much

B - Nov 27th 2005, 04:50 PMhpeQuote:

Originally Posted by**braddy**

*This is an individual project. You are encouraged to discuss verbally the project with other students. You can brainstorm together solution approaches, and you can teach each other how to do things with Matlab. However, allowed collaboration ends with this verbal discussion.***At no time can you copy work others have done, or have someone else do any of the work for you, or do any of the work for someone else. Everything in the Matlab files themselves must be your work, and your work alone.**If you need more help, ask your proctor or instructor for assistance.

I think we should all respect these instructions. - Nov 28th 2005, 08:12 AMCaptainBlackQuote:

Originally Posted by**hpe**

text books titled something like: "Numerical Maths with Matlab", it is

tantamount to an instruction not to visit the library :eek:

RonL