Can't integrate function using Matlab
2 次查看(过去 30 天)
显示 更早的评论
I am trying to do a basic integration of a formula for 4 different values. After doing a bit of reading, apparently I need to create a function handle but I am not getting any success. I have put my attempt below. I might be missing more though.
L=0:100;
L_bar=25;
sigma_d =[0.25, 0.5, 0.75, 1.0];
mu = log(L_bar) - 0.5*(sigma_d).^2;
L_tilda = exp(mu);
% eta_r = P.*exp(-r./L);
% I want to integrate eta from zero to infinity where P has changing
% variables to give 4 different plots.
r = 0:100;
% I was just testing below to see if a loop would run before attempting to
% plot anything
P = zeros(1,length(L));
fun = @(r) P.*exp(-r./L);
for ii=1:numel(sigma_d)
P= 1./(L.*sqrt(2*pi)*sigma_d(ii)).*exp(-log(L/L_tilda(ii)).^2/(2*sigma_d(ii)^2)) ;
eta_r = integral(fun,0,inf);
end
0 个评论
采纳的回答
Torsten
2023-2-9
L_bar=25;
sigma_d =[0.25, 0.5, 0.75, 1.0];
mu = log(L_bar) - 0.5*(sigma_d).^2;
L_tilda = exp(mu);
r = 0:100;
for ii=1:numel(sigma_d)
P= @(L)1./(L.*sqrt(2*pi)*sigma_d(ii)).*exp(-log(L/L_tilda(ii)).^2/(2*sigma_d(ii)^2));
fun = @(L,r) P(L).*exp(-r./L);
eta_r(ii,:)=integral(@(L)fun(L,r),0,Inf,'ArrayValued',true);
end
plot(r,eta_r)
grid on
4 个评论
Dyuman Joshi
2023-2-9
You will have to apply that individually -
L=0:100;
L_bar=25;
sigma_d =[0.25, 0.5, 0.75, 1.0];
mu = log(L_bar) - 0.5*(sigma_d).^2;
L_tilda = exp(mu);
r = 0:100;
LineTypes={'-' '--' ':' '-.'};
for ii=1:numel(sigma_d)
P= @(L)1./(L.*sqrt(2*pi)*sigma_d(ii)).*exp(-log(L/L_tilda(ii)).^2/(2*sigma_d(ii)^2));
fun = @(L) P(L).*exp(-r./L);
eta_r(ii,:)=integral(@(L)fun(L),0,Inf,'ArrayValued',true);
plot(r,eta_r(ii,:),'LineStyle', LineTypes{ii}, 'LineWidth', 1.5)
hold on
end
xlabel('r (\mu m)')
xticks([0 20 40 60 80 100])
ylabel('\eta(r)');
ylim([0 1])
title('Spatial Correlation Function LBAR=25')
set(gca, 'FontSize',10);
legend('\sigma_d =0.25', '\sigma_d= 0.5', '\sigma_d =0.75', '\sigma_d = 1')
更多回答(1 个)
Dyuman Joshi
2023-2-9
You will have to explicitly change the function handle to change its definition
P=pi;
fun = @(x) P*x;
P=3;
%fun(3) is not equal to 3*3=9
fun(3)
L=0:100;
L_bar=25;
sigma_d =[0.25, 0.5, 0.75, 1.0];
mu = log(L_bar) - 0.5*(sigma_d).^2;
L_tilda = exp(mu);
% eta_r = P.*exp(-r./L);
% I want to integrate eta from zero to infinity where P has changing
% variables to give 4 different plots.
r = 0:100;
% I was just testing below to see if a loop would run before attempting to
% plot anything
for ii=1:numel(sigma_d)
P= 1./(L.*sqrt(2*pi)*sigma_d(ii)).*exp(-log(L/L_tilda(ii)).^2/(2*sigma_d(ii)^2));
fun = @(x) P.*exp(-x./L);
eta_r=integral(fun,0,Inf,'ArrayValued',true)
end
Since you are dividing by 0 (first element of L), you get a NaN and thus the warning from the integral() solver.
3 个评论
Dyuman Joshi
2023-2-9
"I need to be integrating with respect to L"
You should have mentioned that before. By the definition of the function handle, I took it as you are integrating w.r.t r
Now as you want to integrate w.r.t L, is there a need to define L=0:100?
Or L is a variable in the definition of P as well?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!