Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab help

  1. Mar 23, 2009 #1
    I want to find q501 for the difference equation:

    qn=qn-1 - 0.001 q2n-2
    n≥2,
    q0=1,
    q1=2

    I thought it's easier to do this on Matlab & I tried to write a script file for this problem:

    Code (Text):
    k=0.001
    q(1)=1
    q(2)=2
    for n=1:502
        q_n=q_n-1-k*(q_n-2)^2
    end
    I don't know why my code isn't working/giving the right answer. Does anyone know what's the problem with this script?
     
  2. jcsd
  3. Mar 23, 2009 #2

    f95toli

    User Avatar
    Science Advisor
    Gold Member

    You are not indexing the array in the right way
    Element n of a vector q is written q(n) in Matlab (with the first element having index 1, not zero as in some other programming languages).

    Try
    Code (Text):

    k=0.001
    q(1)=1
    q(2)=2
    for n=3:502
        q(n)=q(n-1)-1-k*q(n-2)^2
    end
     
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Matlab help
  1. MATLAB help (Replies: 9)

  2. MATLAB help (Replies: 2)

  3. MATLAB help (Replies: 0)

  4. MATLAB help (Replies: 7)

  5. Matlab help (Replies: 6)

Loading...