1. Not finding help here? Sign up for a free 30min 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!

MATLAB: input arrays into function

  1. Feb 6, 2015 #1
    1. The problem statement, all variables and given/known data

    Untitled.png
    2. Relevant equations


    3. The attempt at a solution
    Okay, so first off I'm just trying to write the code for scalars:
    --------------------------------------
    clc ; clear

    P = 2000
    r = .035/12
    n = 36

    F = (1 + r).^n

    A = (r*P*F)/(F-1)
    ---------------------------------------

    This gives me A = 58.6042
    which obviously isn't the same as the 586.0416 that they get in the example.
     
  2. jcsd
  3. Feb 6, 2015 #2

    Mark44

    Staff: Mentor

    Check the value you're using for the loan amount P.
     
  4. Feb 6, 2015 #3
    Ah, haha. Always something like that. Now for the actual problem that I was having; how do I make it possible for n to be an array and A to display an array of answers?
     
  5. Feb 6, 2015 #4

    Mark44

    Staff: Mentor

    Maybe something like this:
    Code (Text):

    clc ; clear
    P = 20000
    r = .035/12
    n = [36 48 60]
    A = zeros[1, 3]  ; Initialize A to [ 0 0 0]
    F = zeros[1, 3] ; Initialize F to [ 0 0 0]

    F = (1 + r) .^ n

    A = (r*P*F)/(F-1)
     
    I don't have Matlab, so I can't guarantee this will work
     
  6. Feb 7, 2015 #5
    The code that you have for scalars is actually already pretty close to what you need. Instead of having n be a scalar, you are going to want to set up n to be a vector [36 48 60].

    From there, all you need to do in the code, is anytime that you are wanting it to do scalar multiplication or division instead of vector multiplication or division, you will need to put a period before it to force MatLab to do the operation item by item.

    Using your work for example:

    F = (1 + r).^n (you already have your .^n here before your exponential, so you have that right).

    This will provide you F as a vector for each of the associate values for n. From there you will just need to take the second part of your code and add a period before any of the operators that you want MatLab to do as a scalar operation (In your case, any operator that is using your new vector F).
     
  7. Feb 7, 2015 #6
    I solved it
    ------------------------
    F = (1 + r).^n

    A = (r*P.*F)./(F-1)
    --------------------------
    Thanks guys.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: MATLAB: input arrays into function
  1. Function and array (Replies: 26)

Loading...