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

Matlab, for loop troubles.

  1. Feb 21, 2008 #1
    Hi, I have this for loop where i want it to iterate for a variable ap = ap-1 -f(x)

    and i want ap-1 to replace ap in each iteration.

    It's pretty simple but i just can't get it to work out. any help would be appreciated and here's my code btw.

    clc
    a=1
    fa=exp(a)*((-a^2)+(2*a)-2)+2.723684615938410;
    fprimea=exp(a)*(a^2);
    ap=a+(fa/fprimea);
    tol=1*(10^-6);
    f=abs(ap-a);
    n=15;

    for k=1:10
    ap=a-(fa/fprimea);
    f=abs(ap-a)
    a=ap
    end
     
  2. jcsd
  3. Feb 21, 2008 #2

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    From the look of it, you need to learn how to write functions. Look them up in the help.

    Right now you're just calculating the fa and fprimea junk once.

    - Warren
     
  4. Feb 21, 2008 #3
    I've been working with those and i keep getting this dang error "index must be a positive integer or logical."

    not sure what that means... any ideas?
     
  5. Feb 21, 2008 #4

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    It probably means you're trying to use 0 to index a vector or matrix. MATLAB indices begin at 1.

    - Warren
     
  6. Feb 21, 2008 #5
    sorry, but im not sure what that means at all :[
     
  7. Feb 21, 2008 #6

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Well, I can't say for sure unless you show me the code and indicate what line is causing the error.

    - Warren
     
  8. Feb 21, 2008 #7
    function [f ap]=NR(fa,fprimea,a,tol,f,iterations)

    a=1
    fa=exp(a)*((-a^2)+(2*a)-2)+2.723684615938410;
    fprimea=exp(a)*(a^2);
    ap=a+(fa/fprimea);
    tol=1*(10^-6);
    f=abs(ap-a);
    n=15;
    iterations= input('how many interations? ')

    for k=1:iterations
    ap=a-(fa(a)/fprimea(a)); <----- error is in this line
    f=abs(ap-a)
    a=ap
    end

    end


    error: ??? Attempted to access fa(0.998012); index must be a positive integer or logical.

    Error in ==> matlabsss at 14
    ap=a-(fa(a)/fprimea(a));
     
  9. Feb 21, 2008 #8

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    In this line:

    ap=a-(fa(a)/fprimea(a));

    Are you just trying to multiply fa by a and fprimea by a? If so, you need to use * to represent multiplication. MATLAB is interpreting your use of parentheses as an array lookup, e.g. fa(a) is the ath element of fa.

    - Warren
     
  10. Feb 21, 2008 #9
    okay, i have a function going and all that and it's still doing the same thing. it's not modifying the a to make it equal to the ap of the last iteration...
     
  11. Feb 21, 2008 #10

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    a only exists inside your function. It disappears as soon as the function is done running. You're passing ap out.

    - Warren
     
  12. Feb 21, 2008 #11
    I got it!

    thanks so much for the help
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Matlab, for loop troubles.
  1. Loop troubles in Maple (Replies: 0)

  2. Matlab solve trouble (Replies: 2)

  3. For loop in matlab (Replies: 3)

Loading...