how can i create a plot for my program? (beginner)
조회 수: 13 (최근 30일)
이전 댓글 표시
Im to create a plot for my program but i dont know if there is actually a way to write a code so that user inputs will be plotted after the program ends since it is dependent on what the user enters in the command window) and if that is possible should i create the plotting codes immediately under my program or am i supposed to be labeling new variables. sorry if the question is confusing. and to summarize my program its just basically asking the user if they want to withdraw or deposit and the program ends after 5 transactions. i need to plot the balances.
balance = input('Enter your starting balance: ');
disp('Thank you.')
transaction = 0;
withdraw = 0;
deposit = 0;
loop = 1;
%initialize loop
while(loop) %This is going to be a continuous loop until it breaks from within.
disp('What would you like to do?');
fprintf('1 to deposit \t 2 to withdraw\n');
fprintf('--------------------------\n');
user = input('Select: ');
if user==1 %User chose deposit
amount = input('Enter amount you want to deposit: ');
balance = balance + amount; %program will calculate sum of amount and balance
transaction = transaction + 1;
deposit = deposit + 1;
fprintf('Your Transaction was successful. Your New balance is %.2f dollars.\n',balance);
fprintf('--------------------------\n');
elseif user==2 %another option selected. this time user chose withdraw from starting balance.
amount = input('Enter the amount you would like to withdraw: ');
withdraw = withdraw + 1;
transaction = transaction + 1;
if (balance < amount)
balance = balance - amount;
fprintf('You overdrew your account. An overdraft charge will be applied.\n');
fprintf('you have an overdraft of %.2f dollars.\n',balance);
else
balance = balance - amount;
fprintf('Your transaction was successful. Your new balance is %.2f dollars.\n', balance);
end
end
%program stops after 5 transactions
if transaction >= 5
fprintf('You have made %d transactions.\n',transaction);
fprintf('Your ending balance is %.2f dollars.\n', balance);
break
end
end %ending while loop
%withdrawal and deposit messages
if withdraw == 5
fprintf('Careful, make sure that you aren’t spending too much!\n')
else deposit == 5
fprintf('Good job on saving your money!\n');
end
disp('end');
댓글 수: 2
Walter Roberson
2020년 4월 17일
Just after you do transaction = transaction + 1; save the amount into a variable indexed at transaction. Save negative for a withdrawl. At the end plot the saved transactions.
You might also be wanting to save the balances as you go. If you do you would probably want to save the original balance first, and index at (transaction+1) as you save the balances, and plot them starting from 0 (time) on the x axis.
답변 (1개)
David Hill
2020년 4월 17일
You just need to vectorize your balance and index into it.
balance = input('Enter your starting balance: ');
disp('Thank you.')
transaction = 0;
withdraw = 0;
deposit = 0;
loop = 1;
%initialize loop
while(loop) %This is going to be a continuous loop until it breaks from within.
disp('What would you like to do?');
fprintf('1 to deposit \t 2 to withdraw\n');
fprintf('--------------------------\n');
user = input('Select: ');
if user==1 %User chose deposit
amount = input('Enter amount you want to deposit: ');
transaction = transaction + 1;
balance(transaction) = balance(transaction-1) + amount; %program will calculate sum of amount and balance
deposit = deposit + 1;
fprintf('Your Transaction was successful. Your New balance is %.2f dollars.\n',balance(transaction));
fprintf('--------------------------\n');
elseif user==2 %another option selected. this time user chose withdraw from starting balance.
amount = input('Enter the amount you would like to withdraw: ');
withdraw = withdraw + 1;
transaction = transaction + 1;
if (balance(transaction-1) < amount)
balance(transaction) = balance(transaction-1) - amount;
fprintf('You overdrew your account. An overdraft charge will be applied.\n');
fprintf('you have an overdraft of %.2f dollars.\n',balance(transaction));
else
balance(transaction) = balance(transaction-1) - amount;
fprintf('Your transaction was successful. Your new balance is %.2f dollars.\n', balance(transaction));
end
end
%program stops after 5 transactions
if transaction >= 5
fprintf('You have made %d transactions.\n',transaction);
fprintf('Your ending balance is %.2f dollars.\n', balance(transaction));
break
end
end %ending while loop
%withdrawal and deposit messages
if withdraw == 5
fprintf('Careful, make sure that you aren’t spending too much!\n')
else deposit == 5
fprintf('Good job on saving your money!\n');
end
plot(transaction,balance);%plots balance per transactions
disp('end');
댓글 수: 2
Walter Roberson
2020년 4월 17일
balance(transaction+1) = balance(transaction) + amount;
That is, balance(1) would be the opening balance, balance(2) would be after one transaction, balance(3) would be after two transactions, and so on.
plot(transaction,balance);%plots balance per transactions
transaction would be a scalar at that point. You need to plot from 0 to the number of transactions.
By the way, we can tell you are not executing in MATLAB based on the error message you got. It is a correct error message but not one that MATLAB would produce.
참고 항목
카테고리
Help Center 및 File Exchange에서 Annotations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!