MATLAB Trapezium Rule - Input Error

7 views (last 30 days)
M P
M P on 9 Dec 2016
Edited: Sanaz Kb on 17 Apr 2018
Hi there,
I have written a mathematical code to calculate the trapezium rule for a set function. For my entire program, I have attempted to make it as 'user-friendly' as possible; whereby all variables are set upon running the code. This way, the script does not need to be editted in any way - see my code if you're confused in any way.
The issue I have is that when I input the actual mathematical 'function' into the program, it keeps being called multiple times, when I only want it to be called once. For example, if the function I needed evaluating was: 2x^2, I would input ' 2*x.^2 ', but would have to do this several times instead of once.
I think that the number of times the program calls upon this input is related to the number of strips to calculate - try this and see if you think I'm right? Therefore, I believe the loop inside the script keeps calling the mathematical 'function' user input continuously - I don't know how to stop this from happening?
I am a beginner to MATLAB and whilst it appears that I'm overcomplicating everything, I'm only trying to make everything as 'user-friendly' as possible...
My script: >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.
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:');
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
% Truth - Integral Calculation
g = @f;
truth = integral(g,a,b);
disp(' ');
disp('Actual Value of Integral:');
disp(truth);
% Final Value of Tra.
Answer = h*sum;
ModAnswer = abs(Answer);
disp('Value of Trapezium Area Calculated:');
disp(ModAnswer);
% 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);
% Functions Formulae
function y = f(x)
y = input('Enter function for processing:');
end
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Please help me on how to solve this issue please?
Thank you!

Accepted Answer

M P
M P on 9 Dec 2016
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

More Answers (2)

Jan
Jan on 9 Dec 2016
The problem is that "f" is called n times, such that the input command appears so often.
Use a function instead of a script. Then provide the function as anonymous function as usual in Matlab (see ode45 or fzero).
Avoid "sum" as name of a variable, because this is an important Matlab function and shadowing it causes unexpected behavior frequently.
  1 Comment
M P
M P on 9 Dec 2016
Hi Jan,
Thank you for your prompt reply!
I somewhat undrestand where you're coming from in terms of 'n' times the input is called.
I'm using R2016b and so I've placed the function within the script at the bottom - is this any different from creating a 'm' (function) file, since I'll still want the function to be 'user-friendly' and be open to the user's choice?
Is there by any chance a way to set a mathematical function once, then call upon the 'function' function and then let that run through the program?
I am a beginner but I've heard of prompts and strings - could this be a solution, i.e. string in some user prompt and set that as a function? If you understand what Im trying to say?
I only used 'sum' because my engineering computation book told me to do so in such a code - I will change the name of this variable, so thank you!
Thank you for your help!

Sign in to comment.


Sanaz Kb
Sanaz Kb on 17 Apr 2018
Edited: Sanaz Kb on 17 Apr 2018
Hello,
I have a question about the accepted code. When I've implemented your code, in the line which is related to the "truth = integral(f,a,b);", I've faced with this error:
Error using integral Too many input arguments.
Error in Untitled12 (line 24) truth = integral(f,a,b);
and also I have another questioin, How can I solve the integral if the upper limit be the vector not scalar? Could you please help me?
Thanks

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!