I need help debugging my code

Ben Abbott bpabbott at mac.com
Tue Apr 28 20:34:13 CDT 2009


On Apr 28, 2009, at 9:09 PM, cdiaz at jacks.sdstate.edu wrote:

> 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'
>
> Thank you for your help!  I'm still pretty new to octave, but I'm  
> getting there!
> Camille

No need for ".-" or ".+", just use "-" and "+".

Regarding the loop

> for i=1:N
>     [t,y] = [t(i),y(i)];
> endfor

I don't know what  you intend to do here. It appears to be unneeded,  
so I just deleted it.

You'll also need to pass the function either a handle or as a text  
string. In the context of your example, I choose to use a text string.

See the attached.

To run it place it in the appropriate directory and from Octave type ...

	[t, y] = tryrk4 ("yprime", 0, 2, 0.5, 0.2)

or just type

	demo tryrk4

Ben


-------------- next part --------------
A non-text attachment was scrubbed...
Name: tryrk4.m
Type: application/octet-stream
Size: 675 bytes
Desc: not available
Url : https://www-old.cae.wisc.edu/pipermail/help-octave/attachments/20090428/a211d118/attachment.obj 
-------------- next part --------------







More information about the Help-octave mailing list