Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Adams-Bashforth-Moulton method

Subject: Adams-Bashforth-Moulton method

From: Emilia Przybysz

Date: 5 Feb, 2013 17:39:58

Message: 1 of 5

Hi, I have a huge problem. I have to write a script that solve differential equation like this
2000y"=98(1+y')^2{t(t-1)+2}
using Adams-Bashforth-Moulton method and then compare results using ode45. I wrote a function

>function dy = belka(t,y)
>% funkcja w której zapisali?my równanie różniczkowe
>dy=zeros(2,1) ;
>dy(1)=y(2);
>dy(2)=(9.8*10/2000)*(1+y(2))^2*{t.*(t-1)+2};
>end

And I have a function

>function [ t, y ] = ode_abm( f, tspan, y0, N, KC, varargin )
>%UNTITLED Summary of this function goes here
>% Detailed explanation goes here
>if nargin<5, KC=1; end
>if nargin<4 | N<=0. N=100; end
>y0=y0(:)';
>h=(tspan(2)-tspan(1))/N;
>tspan0=tspan(1)+[0 3]*h;
>[t, y]=rk4(f, tspan0, y0, 3, varargin{:});
>t=[t(1:3)' t(4):h:tspan(2)]';
>for k=1:4, F(k,:)=feval(f, t(k), y(k,:), varargin{:}); end
>p=y(4,:); c=y(4,:); KC22=KC*251/270; KC12=KC*19/270;
>h24=h/24; h241=h24*[1 -5 19 9]; h249=h24*[-9 37 -59 55];
>for k=4:N
> p1=y(k,:)+h249*F;
> m1=pk1+KC22*(c-p);
> c1=y(k,:)+h241*[F(2:4,:); feval(f,t(k+1), m1, varargin{:})];
> y(k+1,:)=c1-KC12*(c1-p1);
> p=p1; c=c1;
> F=[F(2:4,:);feval(f,t(k+1),y(k+1,:), varargin{:})];
>end
>end
 
It has inside function rk4, so

function [ t, y ] = rk4( f, tspan, y0, N, varargin )
%UNTITLED8 Summary of this function goes here
% Detailed explanation goes here
if nargin<4 | N<=0, N=100; end
if nargin<3, y0=0; end
y(1,:)=y0(:)';
h=(tspan(2)-tspan(1))/N; t=tspan(1)+[0;N]'*h;
for k=1:N
    f1=h.*feval(f,t(k),y(k,:),varargin{:}); f1=f1(:)';
    f2=h.*feval(f,t(k)+h/2,y(k,:)+f1/2,varargin{:}); f2 = f2(:)';
    f3=h.*feval(f,t(k)+h/2,y(k,:)+f2/2,varargin{:}); f3 = f3(:)';
    f4=h.*feval(f,t(k)+h,y(k,:)+f3,varargin{:}); f4 = f4(:)';
    y(k+1,:)=y(k,:)+(f1+2.*(f2+f3)+f4)/6;
end
 
But i doesn't work. Matlab says

>> [t, y]=ode_abm(@belka, [0 2], [0 0], 100, 1)
??? Undefined function or method 'mtimes' for input arguments of
type 'cell'.

Error in ==> belka at 5
dy(2)=(9.8*10/2000)*(1+y(2))^2*{t.*(t-1)+2};

Error in ==> rk4 at 9
    f1=h.*feval(f,t(k),y(k,:),varargin{:}); f1=f1(:)';

Error in ==> ode_abm at 9
[t, y]=rk4(f, tspan0, y0, 3, varargin{:});

What is wrong? Could anyone help me? That means a lot for me. I am really bad it Matlab, so please- give me simple explanation ;)

Subject: Adams-Bashforth-Moulton method

From: Steven_Lord

Date: 5 Feb, 2013 18:12:32

Message: 2 of 5



"Emilia Przybysz" <emilia.przybysz0909@gmail.com> wrote in message
news:46eeeba3-e22b-4db4-8726-6cb19b69a47d@googlegroups.com...
> Hi, I have a huge problem. I have to write a script that solve
> differential equation like this
> 2000y"=98(1+y')^2{t(t-1)+2}
> using Adams-Bashforth-Moulton method and then compare results using ode45.
> I wrote a function
>
>>function dy = belka(t,y)
>>% funkcja w ktrej zapisali?my rwnanie rniczkowe
>>dy=zeros(2,1) ;
>>dy(1)=y(2);
>>dy(2)=(9.8*10/2000)*(1+y(2))^2*{t.*(t-1)+2};

The section:

{t.*(t-1)+2}

creates a cell array. MATLAB doesn't know how to multiply the previous term
in the definition for dy(2):

(9.8*10/2000)*(1+y(2))^2

and a cell array -- it's not defined. Use parentheses instead of curly
braces if all you want to do is group together the expression representing
"t times t-1, plus 2", just like you used parentheses to group the
expression "9.8 times 10 divided by 2000."

*snip*

>>[t, y]=rk4(f, tspan0, y0, 3, varargin{:});

Here and in the similar function evaluation calls later in your code curly
braces ARE needed, as you're working with the cell array VARARGIN.

*snip*

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Adams-Bashforth-Moulton method

From: Emilia Przybysz

Date: 5 Feb, 2013 18:46:12

Message: 3 of 5

Thanks :)
I changed curly brases to parentheses and that's ok- I checked with ode113.
But I still don't know what should I do with your second piece of advice. Where should I put curly brases? How to change this part
>>[t, y]=rk4(f, tspan0, y0, 3, varargin{:});

Subject: Adams-Bashforth-Moulton method

From: Emilia Przybysz

Date: 5 Feb, 2013 18:52:37

Message: 4 of 5

Thanks :)
I changed curly braces to parentheses and that's ok- I checked with ode113.
But I still don't know what should I do with your second piece of advice. Where should I put curly braces? How to change this part
>>[t, y]=rk4(f, tspan0, y0, 3, varargin{:});

Subject: Adams-Bashforth-Moulton method

From: Christopher Creutzig

Date: 7 Feb, 2013 08:24:22

Message: 5 of 5

On 05.02.13 19:52, Emilia Przybysz wrote:
> Thanks :)
> I changed curly braces to parentheses and that's ok- I checked with ode113.
> But I still don't know what should I do with your second piece of advice. Where should I put curly braces? How to change this part
>>>[t, y]=rk4(f, tspan0, y0, 3, varargin{:});
>

Don't change it. Steven just said you should not change all curly braces
you have anywhere, since in this place, they are exactly what you need.


HTH,
Christopher

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us