Solved it!
The best way is to NOT use the function script at the bottom of the file...
Instead, assign a variable and use an input function along with it - see code below!
Here, adding >> @(x) << before whatever you input stores it as a function, and the following code will do its job as normal!
Thanks for your help anyways!
disp('Trapezium Rule Calculator');
disp('Truth and Absolute Values');
disp('-------------------------');
disp(' ');
% F. Variables
a = input('Enter the value of the lower bound: ');
b = input('Enter the value of the upper bound: ');
n = input('Enter the number of strips required: ');
f = input('Enter function: [@(x)...]: ');
disp(' ');
h = ((b - a)/ (n));
% Summation of First and Last Tra.
sum = 0.5*( f(a) + f(b) );
% Loop for Middle Tra.s
for i = 1 : n-1;
sum = sum + f(a+i*h);
end
% Final Value of Tra.
Answer = h*sum;
ModAnswer = abs(Answer);
disp('Value of Trapezium Area Calculated:');
disp(ModAnswer);
% Truth - Integral Calculation
truth = integral(f,a,b);
disp('Actual Value of Integral:');
disp(truth);
% Difference (Accuracy)
ModTruth = abs(truth);
diff = abs( ModTruth - ModAnswer);
disp('Difference in Calculation:');
disp(diff);
% Percentage Accuracy
format short
PInaccuracy = ( (diff) / (ModTruth) )*100;
PA = 100 - PInaccuracy;
disp('Percentage Accuracy (%):');
disp(PA);
For example:
Trapezium Rule Calculator
Truth and Absolute Values
-------------------------
Enter the value of the lower bound: 1
Enter the value of the upper bound: 2
Enter the number of strips required: 16
Enter function: [@(x)...]: @(x) 1./x
Value of Trapezium Area Calculated:
0.6934
Actual Value of Integral:
0.6931
Difference in Calculation:
2.4402e-04
Percentage Accuracy (%):
99.9648