function napaka(fun,a,b,tocna,n1,n2,d) %napaka(fun,a,b,n1,n2,d) %narise graf napake pri numericnem integriranju s pomocjo %trapezne in Simpsonove metode. %Primerja tocno vrednost integrala %int_a^b fun(x)dx, %ki jo podamo s spremenljivko 'tocna' %z rezultati funkcij 'trapez.m' in 'simpson.m' za stevilo podintervalov, %ki gre od 'n1' do 'n2' s korakom 'd' in narise graf. %Plots the errors of numerical integration methods of trapez.m and simpson.m %We compare the exact value of the integral %int_a^b fun(x)dx, %given by the variable 'tocna' (which we must know in advance) to the results of the %functions trapez.m and simpson.m for numbers of subintervals from 'n1' to 'n2' %with step 'd' n=n1:d:n2;%vrednosti za parameter 'n', ki nas zanimajo // values of 'n' l=length(n);%stevilo klicov trapez.m oz. simpson.m // number of calls of the functions trapez.m and simpson.m It=zeros(1,l);%rezerviramo prostor za rezultate // space for the results Is=zeros(1,l); for i=1:l It(i)=trapez(fun,a,b,n(i));%racunamo priblizke integralov // computing numerical approximations of integrals Is(i)=simpson(fun,a,b,n(i)); end clf; hold on plot(n,abs(It-tocna),'b');%graf napake trapezne metode // error plot for trapez.m plot(n,abs(Is-tocna),'r');%graf napake simpsonove metode // error plot for simpson.m