help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

I need help debugging my code


From: cdiaz
Subject: I need help debugging my code
Date: Tue, 28 Apr 2009 20:09:30 -0500

I’ve been working on this code for a week now, but I still keep getting error messages.  Can anyone help?  Thanks!

Here is my outside function file:

 

function f = yprime(t,y)

y(1) = 0.5;

t(1) = 0;

 

                        f = y .- t.^2 .+ 1

                       

endfunction

 

Here is my first code and error message:

function [t,y] = tryrk4(ydot, a, b, y0, h);

i=1;

t(1) = a;

y(1) = y0;

N = 10;

 

for i=1:N

   k1 = feval(ydot, t(i)    , y(i)       );

   k2 = feval(ydot, t(i)+h/2, y(i)+k1*h/2);

   k3 = feval(ydot, t(i)+h/2, y(i)+k2*h/2);

   k4 = feval(ydot, t(i)+h  , y(i)+k3*h  );

 

   i = i+1;

   t(i) = a+i*h

 

   y(i) = y(i-1) + (k1 + 2*k2 + 2*k3 + k4)*h/6

  

 

endfor

for i=1:N

                        [t,y] = [t(i),y(i)]

endfor

endfunction

octave-3.0.2.exe:4:C:\Program Files\Octave\3.0.2_gcc-4.3.0\bin

> tryrk4(yprime, 0,2,.5,.2)

f =  1.5000

 

Here’s the error message:

error: octave_base_value::function_value(): wrong type argument `scalar'

error: evaluating assignment _expression_ near line 18, column 7

error: evaluating for command near line 17, column 1

error: called from `tryrk4' in file `C:\Program Files\Octave\3.0.2_gcc-4.3.0\bin\tryrk4.m'

octave-3.0.2.exe:4:C:\Program Files\Octave\3.0.2_gcc-4.3.0\bin

 

So I tried changing things to vectors, and this is what I have now:

function [t,y] = keeptrying(ydot, a, b, y0, h);

% solution of 1st order ODE using Runge-Kutta 4th order

%

%ydot,a,b,y0,h

%

% input  ydot    = name of external function, differential equation

%        a, b    = limits of integration

%        y0      = initial condition

%        h       = stepsize

%

% output [t, y]  = solution vectors

i=1;

[t(1),y(i)] = [a,y0]

 

N = 10;

 

for i=1:N

   k1 = feval(ydot, [t(i)    , y(i)]       );

   k2 = feval(ydot, [t(i)+h/2, y(i)+k1*h/2]);

   k3 = feval(ydot, [t(i)+h/2, y(i)+k2*h/2]);

   k4 = feval(ydot, [t(i)+h  , y(i)+k3*h]  );

 

   i = i+1;

   [t,y] = [(a+i*h),(y(i-1) + (k1 + 2*k2 + 2*k3 + k4)*h/6)]

 

 

 

endfor

 

endfunction

 

 

And I get this error:

octave-3.0.2.exe:4:C:\Program Files\Octave\3.0.2_gcc-4.3.0\bin

> keeptrying(yprime, 0,2,.5,.2)

f =  1.5000

error: invalid number of output arguments for matrix list

error: called from `keeptrying' in file `C:\Program Files\Octave\3.0.2_gcc-4.3.0\bin\keeptrying.m'

octave-3.0.2.exe:4:C:\Program Files\Octave\3.0.2_gcc-4.3.0\bin

> 

 

Thank you for your help!  I'm still pretty new to octave, but I'm getting there!

Camille


reply via email to

[Prev in Thread] Current Thread [Next in Thread]