I’m kinda confused with the smart plotter , here is some example of it

6 次查看(过去 30 天)
Smart Plotter Program - Design an application that displays a menu to ask the user to choose whether to: (20 marks) (a) Plot the relationship between the temperature and time using bar chart using the following instructions − Load a file that contain an array of 10 days − Load a file that contain an array of 10 corresponding sales amounts at which the sales were done. − Determine the total sales and print the output. (b) Plot on the same graph for the functions f(x) = x + 3, g(x) = X2 +1, f(x)*g(x) and f(x)/g(x) for the range x values from -1 to 1. (c) Quit the Smart Plotter The user can choose then proceed to plot a simple 2D line, a scatter plot or bar chart. The program will continue displaying the menu to allow the user to choose until the user chooses to quit the application

回答(1 个)

Diwakar Diwakar
Diwakar Diwakar 2023-6-11
Try this code:
function smart_plotter()
while true
disp("---- Smart Plotter Menu ----")
disp("1. Plot Temperature vs Sales")
disp("2. Plot Functions")
disp("3. Quit")
choice = input("Enter your choice (1-3): ");
switch choice
case 1
plot_temperature_sales();
case 2
plot_functions();
case 3
disp("Exiting Smart Plotter...");
break;
otherwise
disp("Invalid choice. Please try again.");
end
end
end
function plot_temperature_sales()
try
temperature_file = input("Enter the file name for temperature data: ", 's');
sales_file = input("Enter the file name for sales data: ", 's');
temperature = load(temperature_file);
sales = load(sales_file);
bar(temperature, sales);
xlabel("Temperature");
ylabel("Sales");
title("Temperature vs Sales");
total_sales = sum(sales);
disp("Total sales: " + total_sales);
pause; % Pauses execution until the user closes the plot window
catch e
disp("Error: " + e.message);
end
end
function plot_functions()
x = linspace(-1, 1, 100);
f = x + 3;
g = x.^2 + 1;
fg = f .* g;
fg_div = f ./ g;
plot(x, f, 'DisplayName', 'f(x) = x + 3');
hold on;
plot(x, g, 'DisplayName', 'g(x) = x^2 + 1');
plot(x, fg, 'DisplayName', 'f(x) * g(x)');
plot(x, fg_div, 'DisplayName', 'f(x) / g(x)');
hold off;
xlabel("x");
ylabel("y");
title("Function Plots");
legend('Location', 'best');
pause; % Pauses execution until the user closes the plot window
end
smart_plotter();
  4 个评论

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by