Comments
Write a Matlab program that will display a menu, which will let the user to:
- Enter a function in symbolic format
- Displays the nth derivative of the function, n is entered/selected by the user - n can ONLY be 1, 2, or 3.
- Calculates and displays the indefinite integral of the entered function
- Plot the function, its first derivative and its integral [use subplots]
- Exit
Make sure:
Error correcting is needed
Sample runs should be included. [need to cover all possiblities]
The answer
Description:
This MATLAB program presents a menu that allows the user to:
-
Enter a symbolic function
-
Display the nth derivative (where n = 1, 2, or 3)
-
Calculate and display the indefinite integral
-
Plot the function, its first derivative, and its integral (with subplots)
-
Exit the program
The program includes error checking for invalid input and demonstrates sample runs for all menu options.
MATLAB Code:
matlab
% Symbolic Function Calculator and Plotter Menu
clc; clear; close all;
syms x;
user_func = [];
func_entered = false;
while true
disp('====== Symbolic Function Menu ======');
disp('1. Enter a function in symbolic format');
disp('2. Display the nth derivative (n = 1, 2, or 3)');
disp('3. Calculate and display the indefinite integral');
disp('4. Plot the function, its first derivative and its integral');
disp('5. Exit');
choice = input('Select an option (1-5): ');
switch choice
case 1
f_input = input('Enter a function of x (e.g., sin(x), x^3+2*x, exp(-x)): ', 's');
try
user_func = str2sym(f_input);
func_entered = true;
disp(['Function entered: ', char(user_func)]);
catch
disp('Invalid function format! Please try again.');
func_entered = false;
end
case 2
if ~func_entered
disp('Please enter a function first (option 1).');
continue;
end
n = input('Enter n for the nth derivative (1, 2, or 3): ');
if isnumeric(n) && ismember(n, [1 2 3])
deriv = diff(user_func, x, n);
disp(['The ', num2str(n), 'th derivative:']);
pretty(deriv);
else
disp('Error: n must be 1, 2, or 3.');
end
case 3
if ~func_entered
disp('Please enter a function first (option 1).');
continue;
end
integral_result = int(user_func, x);
disp('The indefinite integral is:');
pretty(integral_result);
case 4
if ~func_entered
disp('Please enter a function first (option 1).');
continue;
end
d1 = diff(user_func, x, 1);
integral_result = int(user_func, x);
fplot(user_func, [-10,10]);
title('Function');
subplot(3,1,1);
fplot(user_func, [-10,10]);
title('Original Function');
grid on;
subplot(3,1,2);
fplot(d1, [-10,10]);
title('First Derivative');
grid on;
subplot(3,1,3);
fplot(integral_result, [-10,10]);
title('Indefinite Integral');
grid on;
case 5
disp('Exiting the program. Goodbye!');
break;
otherwise
disp('Invalid choice. Please select from 1 to 5.');
end
disp(' '); % Blank line for readability
end
Sample Runs:
Sample Run 1: (Try invalid function, then valid one)
====== Symbolic Function Menu ======
1. Enter a function in symbolic format
...
Select an option (1-5): 1
Enter a function of x (e.g., sin(x), x^3+2*x, exp(-x)): sin(x
Invalid function format! Please try again.
Select an option (1-5): 1
Enter a function of x (e.g., sin(x), x^3+2*x, exp(-x)): sin(x)
Function entered: sin(x)
Sample Run 2: (Try derivative before entering function)
Select an option (1-5): 2
Please enter a function first (option 1).
Sample Run 3: (Show 2nd derivative of sin(x))
Select an option (1-5): 2
Enter n for the nth derivative (1, 2, or 3): 2
The 2th derivative:
-sin(x)
Sample Run 4: (Display indefinite integral)
Select an option (1-5): 3
The indefinite integral is:
-cos(x)
Sample Run 5: (Plot function, derivative, and integral)
Select an option (1-5): 4
-- [Shows figure with three subplots: sin(x), cos(x), -cos(x)] --
Sample Run 6: (Exit)
Select an option (1-5): 5
Exiting the program. Goodbye!
Note:
-
The program checks for valid function input and menu choices.
-
Derivative order must be 1, 2, or 3.
-
The plot option uses
subplot
to show all curves on the same figure.
📩 Need a similar solution? Email me: adel455@hotmail.com