Why does my function give weird outputs??
Afficher commentaires plus anciens
I am trying to create a code to calculate work. I have a vector for n and when I run the script I keep getting numbers that don't make sense such as a zero for the estimated integral when using the vector. when I run the script without the n vector and just input n I get more logical answers but they still seem off.
function [ w,Ws,error ] = trap_wfp610( ~ )
% Calculates the work done using numerical integration and the trapezoidal rule
% Detailed explanation goes here
n=[1,11,21,3,1,41,51]';
h=(5*10^-2)/n;
a=0;
b=5*10^-2;
f=@(a)(5*(10^5)*a+10^5)*pi*(5*10^-2)^2;
g=@(b)(5*(10^5)*b+10^5)*pi*(5*10^-2)^2;
w=h/2*(f(a)+g(b));
syms x
k1=500000;
k2=10^5;
p=(k1*x+k2)*pi*.05^2;
Ws=int(p,x,0,5*10^-2);
error=abs((w-Ws)/Ws)*100;
disp('Work Trap Rule');
disp(w);
disp('Work Actual');
disp(Ws);
disp('Error');
disp(error);
plot(w,n)
plot(error,n)
end
These are my outputs
>> trap_wfp610
Work Trap Rule
0 0 0 0 0 0 0.8662
Work Actual
(225*pi)/16
Error
[ 100, 100, 100, 100, 100, 100, 5000/51]
ans =
0 0 0 0 0 0 0.8662
which seem illogical.
1 commentaire
John D'Errico
le 11 Déc 2014
Please learn to format your code so it becomes readable.
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Mathematics dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!