Azzera filtri
Azzera filtri

Integral with limit variable

29 visualizzazioni (ultimi 30 giorni)
GrEG
GrEG il 14 Gen 2024
Risposto: GrEG il 15 Gen 2024
I know the problem is old and well known. However, I'm a beginer and I cannot solve it in my particular issue:
beta=2.1;
eta=1500;
t=0:5000;
R=exp(-(t./eta).^beta);
fun=@(t)exp(-(t./eta).^beta);
INT=integral(fun,0,t);
Error using integral
Limits of integration must be double or single scalars.
I have no idea how to solve this issue with returning the values of my function (fun) within a given range of t, not olny for single or double scalars. Is there any way?

Risposta accettata

Torsten
Torsten il 14 Gen 2024
Modificato: Torsten il 14 Gen 2024
Note that t is at the same time integration variable and upper limit of the integral. That's confusing. You should rename one of these variables.
beta=2.1;
eta=1500;
t=0:5000;
fun=@(t)exp(-(t./eta).^beta);
INT=arrayfun(@(t)integral(fun,0,t),t)
INT = 1×5001
0 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 8.9999 9.9999 10.9999 11.9998 12.9998 13.9998 14.9997 15.9996 16.9995 17.9995 18.9994 19.9993 20.9991 21.9990 22.9989 23.9987 24.9985 25.9983 26.9981 27.9979 28.9976
plot(t,INT)

Più risposte (3)

Anjaneyulu Bairi
Anjaneyulu Bairi il 14 Gen 2024
Modificato: Anjaneyulu Bairi il 14 Gen 2024
Hi,
I understand that you are trying to solve integration problem, but facing errors with limits of integration. You can try below troubleshooting steps to resolve your query.
  • The variable "t" is an array but this argument should be a scalar and if you want to find integration for every value in "t" from 0 , you can refer the below code .
beta=2.1;
eta=1500;
t=0:5000;
R=exp(-(t./eta).^beta);
fun=@(t)exp(-(t./eta).^beta);
for i=1:length(t); %loop on t
INT(i)=integral(fun,0,t(i));
end
  • To plot between "t" and "INT", you can execute the below command
plot(t,INT);
I hope it helps to resolve your query.

John D'Errico
John D'Errico il 14 Gen 2024
Modificato: John D'Errico il 15 Gen 2024
Multiple ways to solve this. First the easy, just as a numerical integration using a trapeziodal rule.
beta=2.1;
eta=1500;
t=0:5000;
R=exp(-(t./eta).^beta);
Rint = cumtrapz(R);
plot(t,Rint)
INT cannot solve the problem in symbolic form. Oh well. I had a funny feeling int might have issues.
syms T
Rsym = exp(-(T./eta).^beta);
int(Rsym)
ans = 
So we see that int gave up. But that does not mean we need to give up too. We can still solve the problem easily enough. Use ode45. Effectively, if we wish to integrate R from 0 to t, this is equivalent to solving the simple first order ODE, of the form
dy/dt = R(t) = exp(-(t./eta).^beta)
You should recognize this as a basic way to perform a cumulative integration. So set this up as an ODE, then apply ODE45 to the problem.
tspan = 0:5000;
y0 = 0;
odefun = @(t,y) exp(-(t./eta).^beta);
[Tout,Yout] = ode45(odefun,tspan,y0);
plot(Tout,Yout)
Again, quite easy to solve. Remember this trick when you need to perform a cumulative integration. Just because you see an integral sign in there, it does not mean you need to use integral or int.

GrEG
GrEG il 15 Gen 2024
Thank you all for the quick and professional answers :)

Community Treasure Hunt

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

Start Hunting!

Translated by