MATLAB: input arrays into function

Click For Summary

Discussion Overview

The discussion revolves around implementing a MATLAB function to handle input arrays, specifically focusing on how to modify existing code to accommodate vector operations for loan calculations. The scope includes homework-related coding challenges and technical explanations of MATLAB syntax.

Discussion Character

  • Homework-related
  • Technical explanation

Main Points Raised

  • One participant presents initial code for calculating loan amounts using scalar values but finds discrepancies in the expected output.
  • Another participant suggests checking the loan amount value used in the calculations.
  • A participant expresses the need to modify the code to allow 'n' to be an array and to output an array of answers for 'A'.
  • Another participant proposes a code structure that initializes arrays for 'A' and 'F', but notes uncertainty about its functionality due to not having MATLAB.
  • A later reply clarifies that the original scalar code is close to the solution and emphasizes the need for element-wise operations in MATLAB by using a period before operators.
  • One participant shares a final solution that successfully implements the required changes to the code for handling arrays.

Areas of Agreement / Disagreement

Participants generally agree on the need to modify the code for vector operations, but there are varying levels of certainty regarding specific implementations and syntax. The discussion remains unresolved regarding the initial discrepancy in the output values.

Contextual Notes

Some limitations include potential misunderstandings of MATLAB syntax and the need for clarity on vector versus scalar operations. The discussion does not resolve the initial output discrepancy.

Feodalherren
Messages
604
Reaction score
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
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.
 
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?
 
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   Reactions: Feodalherren
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   Reactions: Feodalherren
I solved it
------------------------
F = (1 + r).^n

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

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
9
Views
2K
Replies
1
Views
2K
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K