MATLAB Trapezium Rule - Input Error
Mostrar comentarios más antiguos
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!
Respuesta aceptada
Más respuestas (2)
Jan
el 9 de Dic. de 2016
0 votos
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 comentario
M P
el 9 de Dic. de 2016
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
Categorías
Más información sobre Function Creation en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!