Public
Edited
Sep 14, 2024
Paused
1 fork
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// x = [32,37,42,47,52,57,62,67,72,77,82,87,92]

// data: x = lambda
x = {
let lambda = [];
for (let i = 0; i < LED_spectrum.length; i++) {
lambda.push(LED_spectrum[i].lambda);
}
return lambda;
}
Insert cell
// y = [749,1525,1947,2201,2380,2537,2671,2758,2803,2943,3007,2979,2992]

// data: y = U
y = {
let U = [];
for (let i = 0; i < LED_spectrum.length; i++) {
U.push(LED_spectrum[i].U);
}
return U;
}
Insert cell
Insert cell
// model = function(x,p){return x.map(function(x_i){return (
// p[0]*Math.exp(-0.5*(Math.pow((x_i-p[1])/p[2],2))) + p[3]
// )})}
model = function(x,p){return x.map(function(x_i){return (
p[0]*Math.exp(-0.5*Math.pow((x_i-p[1])/p[2],2)) +
p[3]*Math.exp(-0.5*Math.pow((x_i-p[4])/p[5],2)) +
p[6]*Math.exp(-0.5*Math.pow((x_i-p[7])/p[8],2)) +
p[9]*Math.exp(-0.5*Math.pow((x_i-p[10])/p[11],2))
)})}
Insert cell
Insert cell
// p_init = [-70000,-90,40,3000];
p_init = [0.9, 365, 5, 0.2, 390, 15, 0.4, 400, 10, 0.2, 410, 15];
Insert cell
Insert cell
fitting = fminsearch(model,p_init,x,y)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// Source: https://github.com/jonasalmeida/fminsearch
fminsearch = function(fun,Parm0,x,y,Opt){
if(!Opt){Opt={}};
if(!Opt.maxIter){Opt.maxIter=1000};
if(!Opt.step){// initial step is 1/100 of initial value (remember not to use zero in Parm0)
Opt.step=Parm0.map(function(p){return p/100});
Opt.step=Opt.step.map(function(si){if(si==0){return 1}else{ return si}}); // convert null steps into 1's
};
if(typeof(Opt.display)=='undefined'){Opt.display=true};
if(!Opt.objFun){Opt.objFun=function(y,yp){return y.map(function(yi,i){return Math.pow((yi-yp[i]),2)}).reduce(function(a,b){return a+b})}} //SSD
var cloneVector=function(V){return V.map(function(v){return v})};
var ya,y0,yb,fP0,fP1;
var P0=cloneVector(Parm0),P1=cloneVector(Parm0);
var n = P0.length;
var step=Opt.step;
var funParm=function(P){return Opt.objFun(y,fun(x,P))}//function (of Parameters) to minimize
// silly multi-univariate screening
for(var i=0;i<Opt.maxIter;i++){
for(var j=0;j<n;j++){ // take a step for each parameter
P1=cloneVector(P0);
P1[j]+=step[j];
if(funParm(P1)<funParm(P0)){ // if parm value going in the righ direction
step[j]=1.2*step[j]; // then go a little faster
P0=cloneVector(P1);
}
else{
step[j]=-(0.5*step[j]); // otherwiese reverse and go slower
}
}
if(Opt.display){if(i>(Opt.maxIter-10)){console.log(i+1,funParm(P0),P0)}}
}
return P0
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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