how can i create a plot for my program? (beginner)

13 visualizaciones (últimos 30 días)
Reila01
Reila01 el 17 de Abr. de 2020
Comentada: Walter Roberson el 17 de Abr. de 2020
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 comentarios
Walter Roberson
Walter Roberson el 17 de Abr. de 2020
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.
Reila01
Reila01 el 17 de Abr. de 2020
i think i would only need the balances plotted though since i will label the x axis with the number of transactions but i dont know how i would get the program to read that i need to have the balances after ever transaction plotted in a graph.

Iniciar sesión para comentar.

Respuestas (1)

David Hill
David Hill el 17 de Abr. de 2020
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 comentarios
Reila01
Reila01 el 17 de Abr. de 2020
i tried this but im getting an error on this part of the code
if (balance(transaction-1) < amount)
and it says
"error: balance(0): subscripts must be either integers 1 to (2^63)-1 or logicals (note: variable 'balance' shadows function)
error: called from"
Walter Roberson
Walter Roberson el 17 de Abr. de 2020
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Annotations en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by