# regular function integration failure

2 views (last 30 days)
fima v on 7 May 2020
Edited: fima v on 9 May 2020
Hello, i have two functions .The first called "fun" (purple in the plot) is integrated great and i get a precise result of 43 from my definite Matlab integral function.
shown in "fun_integral" expression.
The second one called "multiplication_formula" (BLUE in the plot) when goes into the same integration explodes and i get NAN in the "mult_integral expression.
Why is that? please help me understand where did i go wrong applying the integration of "multiplication_formula" .
I have the code bellow and csv file attached.
Thanks.
%Default Dataset_new file
data = load('Default Dataset4.csv');
x = data(:,1);
y = data(:,2);
%plot(x,y);
data_x1=x*0.000001; %transform to micron
[data_k, index] = unique(data_x1);
coef_fun = @(lambda) interp1(data_k, y(index), lambda); %%%%FIRST FUNCTION
%lambda = linspace(3.5e-6,23e-6,100000);
%plot(lambda, coef_fun(xq))
%title('interp1')
multiplication_formula=@(lambda)fun(lambda).*coef_fun(lambda);
lambda = linspace(3.5e-6,23e-6,100000);
plot(lambda, multiplication_formula(lambda));
hold on
plot(lambda,fun(lambda),'blue');
hold off
fun_integral= integral(fun,3.5e-6,23.5e-6)/10;
mult_integral= integral(multiplication_formula,3.5e-6,23.5e-6)/10;

Ameer Hamza on 7 May 2020
This happens when the value of lambda in integral goes beyond the values of 'data_k' used in interp1. By default, it gives NaN values in that case. You can specify it to extrapolate the values beyond the specified range.
coef_fun = @(lambda) interp1(data_k, y(index), lambda, 'linear', 'extrap');
##### 2 CommentsShowHide 1 older comment
Ameer Hamza on 7 May 2020
I am glad to be of help.

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by