MATLAB: input arrays into function

In summary, the conversation discusses how to write code for scalars using variables P, r, and n. The code provided gives a value for A that is different from the expected value. The solution involves setting n as a vector and adding a period before operators that should be done as scalar operations.
  • #1
Feodalherren
605
6

Homework Statement



Untitled.png

Homework Equations

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.
 
Physics news on Phys.org
  • #2
Feodalherren said:

Homework Statement



Untitled.png

Homework Equations

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.
Check the value you're using for the loan amount P.
 
  • #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?
 
  • #4
Maybe something like this:
Code:
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
 
  • Like
Likes Feodalherren
  • #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).
 
  • Like
Likes Feodalherren
  • #6
I solved it
------------------------
F = (1 + r).^n

A = (r*P.*F)./(F-1)
--------------------------
Thanks guys.
 

1. How do I input an array into a function in MATLAB?

To input an array into a function in MATLAB, you can use the syntax "functionName(arrayName)" where "functionName" is the name of your function and "arrayName" is the name of your array.

2. Can I pass multiple arrays as inputs to a function in MATLAB?

Yes, you can pass multiple arrays as inputs to a function in MATLAB by separating them with commas inside the parentheses, like this: "functionName(array1, array2, array3)".

3. Can I modify the input array inside the function?

Yes, you can modify the input array inside the function by using its name and index notation. Any changes made to the array inside the function will also be reflected outside of the function.

4. How do I return an array from a function in MATLAB?

To return an array from a function in MATLAB, you can use the "return" keyword followed by the name of the array. For example: "return arrayName".

5. Can I use different types of arrays as inputs to a function in MATLAB?

Yes, you can use different types of arrays as inputs to a function in MATLAB. This includes numerical arrays, character arrays, and cell arrays. You can also mix different types of arrays as inputs to a single function.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
735
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
896
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
873
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
742
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
Back
Top