1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Matlab help, function handle

  1. Feb 9, 2010 #1
    1. The problem statement, all variables and given/known data
    Hi,

    I'm writing a little Matlab function that solves an equation with the help of a function handle.The equation is

    x(t) = exp(-a*t)*x0 + exp(-a*t) * int(exp(a*s)*y(s), s=0..t)

    I have tried it with two following functions

    2. The attempt at a solution

    function #1
    function ode = ode1sol(a, x0, y, tol)

    % if tol is not given
    if nargin == 3
    tol = 1e-6;
    end

    t = 0:1:10;

    % sub3 being a subfunction
    ode = sub3(a,x0,y,t,tol);

    function #2
    function x = sub3(a,x0,y,t,tol)

    f = @(s) exp(a*s) .* y(s);

    for i = 1:length(t)
    x(i) = exp(-a*t(i))*x0 + exp(-a*t(i)) .* quad(f,0,t(i),tol);
    end

    --
    when I write
    t=0:1:10;
    ode1sol(1,1,sin(6*t))

    Matlab gives me this error
    ??? Subscript indices must either be real positive integers or logicals.

    Error in ==> sub3>@(s)exp(a*s).*y(s) at 5
    f = @(s) exp(a*s) .* y(s);

    Error in ==> quad at 77
    y = f(x, varargin{:});

    Error in ==> sub3 at 8
    x(i) = exp(-a*t(i))*x0 + exp(-a*t(i)) .* quad(f,0,t(i),tol);

    Error in ==> ode1sol at 13
    ode = sub3(a,x0,y,t,tol);

    I'm confusing the indices somehow but I don't know why...
     
  2. jcsd
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Can you offer guidance or do you also need help?
Draft saved Draft deleted