I am trying to alter this code which I have included and is functioning to use an external function.
After a few attempts but always returning errors I am at an end.
I have tried replacing line 8 and 9 with a function look up and recreating the function in the external file but it throws errors.
I believe it is a simple fix but am a bit stuck.
clear; close all; clc
balance(1) = 1000;
contribution = 1200;
M = 1; % month ticker
interest = 0.0025;
while true %this makes the program run until the break condition is met
M = M + 1; % you need to increment months + 1 every cycle
balance(M) = balance(M-1) + interest*balance(M-1) + contribution;
if balance(end) > 500000;
break;
end
end
years = M/12; %year counter
fprintf('%g Months until $500000 reached\n', M);
fprintf('%g Years until $500000 reached\n', years);
format bank
plot(balance);
grid on
ytickformat('usd')
title('Saving 3% compound')
xlabel('Months')
ylabel('Saving balance')
Label = max(balance) %this labels the plot with max balance
txt = {'Max Balance:',Label,'\leftarrow'};
text(M,Label,txt,'HorizontalAlignment','right')

댓글 수: 4

Cris LaPierre
Cris LaPierre 2021년 9월 18일
What is the external function you are trying to use? What is that fuction supposed to do? Can you share it? You can attach it here using the paperclip icon. What are the error messages you are getting? Please share the entire error message (all the red text).
Chett Manly
Chett Manly 2021년 9월 18일
The external function is to do exactly what this script does. To replace what is in the while loop with a command to use the same 'balance(M) = balance(M-1) + interest*balance(M-1) + contribution;' from an external function.
Image Analyst
Image Analyst 2021년 9월 18일
Please attach the m-file balance.m -- which I guess is your external function.
This is the code I am trying to make work the same as the original post but this one has to use the function externally.
clear; close all; clc
balance(1) = 1000;
contribution = 1200;
M = 1; % month ticker
interest = 0.0025;
while true %this makes the program run until the break condition is met
M = M + 1; % you need to increment months + 1 every cycle
account(balance, contribution, interest, M)
if balance(end) > 500000;
break;
end
end
years = M/12; %year counter
fprintf('%g Months until $500000 reached\n', M);
fprintf('%g Years until $500000 reached\n', years);
format bank
plot(balance);
grid on
ytickformat('usd')
title('Saving 3% compound')
xlabel('Months')
ylabel('Saving balance')
Label = max(balance) %this labels the plot with max balance
txt = {'Max Balance:',Label,'\leftarrow'};
text(M,Label,txt,'HorizontalAlignment','right')
This is my external function file.
function account(balance, contribution, interest, M);
balance(M) = balance(M-1) + interest*balance(M-1) + contribution;

댓글을 달려면 로그인하십시오.

 채택된 답변

Walter Roberson
Walter Roberson 2021년 9월 18일

0 개 추천

in MATLAB, assigning to one of the input parameters only causes local changes, not change to variable passed in. To change the variable you need to return the new value and assign it to the appropriate location in the calling function.

댓글 수: 3

Chett Manly
Chett Manly 2021년 9월 18일
But every time the while loops it should update the value because the line of code is asking for balance, contribution, interest, M ?
clear; close all; clc
balance(1) = 1000;
contribution = 1200;
M = 1; % month ticker
interest = 0.0025;
while true %this makes the program run until the break condition is met
M = M + 1; % you need to increment months + 1 every cycle
balance = account(balance, contribution, interest, M)
if balance(end) > 500000;
break;
end
end
years = M/12; %year counter
fprintf('%g Months until $500000 reached\n', M);
fprintf('%g Years until $500000 reached\n', years);
format bank
plot(balance);
grid on
ytickformat('usd')
title('Saving 3% compound')
xlabel('Months')
ylabel('Saving balance')
Label = max(balance) %this labels the plot with max balance
txt = {'Max Balance:',Label,'\leftarrow'};
text(M,Label,txt,'HorizontalAlignment','right')
function balance = account(balance, contribution, interest, M);
balance(M) = balance(M-1) + interest*balance(M-1) + contribution;
Chett Manly
Chett Manly 2021년 9월 21일
I was so close... Sorry for the late reply, this is exactly it. Thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2021년 9월 18일

댓글:

2021년 9월 21일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by