% example on how to use an external script function
% (put the file diff_first.m i nthe same directory
% where you run this script)

clear all
close all
clc
% script to evaluate the first derivative of a function
% in a given interval
% and compare the numerical result with the exact one
a=0; b=1; N=20;
fun=@(x)(x.^2+5*x.^5);
df_exact=@(x)(2*x+25*x.^4)

% call the external script function diff_first.m
% a, b, N, fun are INPUTs given above
% x, fp, deltax, il, ir are OUTPUTs
[x, fp, deltax, il, ir]=diff_first(a, b, N, fun);

subplot(2,1,1)
hold on
plot(x(il),fp,'r-o')
plot(x(ir),fp,'g-*')
plot(x(il)+deltax/2,fp,'ks')
plot(x,df_exact(x),'k--')
legend('approx 1', 'approx 2', 'approx 3', 'exact','Location','NorthWest')
hold off

subplot(2,1,2)
hold on
plot(x,fun(x),'b-','Linewidth', 2)
legend('function''Location','NorthWest')
hold off