Published
Edited
Jun 11, 2022
Insert cell
Insert cell
/**
* @param {Function} f una funzione continua e derivabile
* @returns {Function} la funzione che calcola il rapporto incrementale finito con incremento positivo
*/
function derivata_t1 (f) {
return (x0) => {
const h = 1E-6;
return ( f(x0 + h) - f(x0) ) / h;
}
}
Insert cell
/**
* @param {Function} f una funzione continua e derivabile
* @returns {Function} la funzione che calcola il rapporto incrementale finito con incremento negativo
*/
function derivata_t2 (f) {
return (x0) => {
const h = -1E-6;
return ( f(x0 + h) - f(x0) ) / h;
}
}
Insert cell
/**
* @param {Function} f una funzione continua e derivabile
* @returns {Function} la funzione derivata che calcola il rapporto incrementale simmetrico
*/
function derivata_t3 (f) {
return (x0) => {
const h = 1E-6;
return ( f(x0 + h) - f(x0 - h) ) / (2 * h);
}
}
Insert cell
/**
* @param {Function} f una funzione continua e derivabile
* @returns {Function} la funzione derivata che calcola il rapporto incrementale simmetrico e incremento effettivo
*/
function derivata_t4 (f) {
return (x0) => {
const h = 1E-6;
const xpiuh = x0 + h;
const xmenoh = x0 - h;
const dueh = xpiuh - xmenoh;
return ( f(xpiuh) - f(xmenoh) ) / (dueh);
}
};
Insert cell
/**
* @param {Function} f una funzione continua e derivabile
* @returns {Function} la funzione derivata che calcola il rapporto incrementale simmetrico, incremento effettivo e scelto per minimizzare l'errore di troncamento
*/
function derivata_t5 (f) {
return (x0) => {
let h = Math.sqrt(Number.EPSILON);
if (Math.abs(x0) > 1.E-6)
h *= x0;
const xph = x0 + h;
const xmh = x0 - h;
const dueh = xph - xmh;
return (f(xph) - f(xmh)) / dueh;
}
};
Insert cell
function testDerivate() {
const derivateNumeriche = [{
diff: derivata_t1,
nome: "T1",
desc: 'incremento positivo prefissato'
}, {
diff: derivata_t2,
nome: "T2",
desc: 'incremento negativo prefissato'
}, {
diff: derivata_t3,
nome: "T3",
desc: 'differenza simmetrica incremento prefissato'
}, {
diff: derivata_t4,
nome: "T4",
desc: 'differenza simmetrica incremento prefissato corretto'
}, {
diff: derivata_t5,
nome: "T5",
desc: 'differenza simmetrica incremento ottimale corretto'
}];

const funzioni = [{
f: x => Math.log(x),
desc: 'log(x)',
f1: x => 1 / x
},
{
f: x => x * x,
desc: 'x^2',
f1: x => 2 * x
},
{
f: x => {
const x2 = x * x;
const x4 = x2 * x2;
return x4 * x4;
},
desc: 'x^8',
f1: x => {
const x2 = x * x;
const x4 = x2 * x2;
return 7 * x * x2 * x4
}
},
{
f: x => Math.exp(x),
desc: 'e^x',
f1: x => Math.exp(x)
}
];

const xs = [1, 10, 100, 1000, 1E10];

let prove = [];
derivateNumeriche.forEach(dn => {
funzioni.forEach(fs => {
xs.forEach(x => {
const funzioneDerivataNumerica = dn.diff(fs.f)
const valoreDerivataNumerica = funzioneDerivataNumerica(x);
const valoreDerivata = fs.f1(x);
const erroreRelativo = Math.abs(valoreDerivata) < 1E-5 ? '-' : Math.abs((valoreDerivata - valoreDerivataNumerica) / valoreDerivata);
prove.push({
algoritmo: dn.nome,
f: fs.desc,
x: x,
valoreDerivataNumerica: valoreDerivataNumerica,
valoreDerivata: valoreDerivata,
errorePerc: 100 * erroreRelativo
});
});
});
});
return prove;
}
Insert cell
Inputs.table(testDerivate())
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more