Integration of piecewise function with integral
Show older comments
Hi, I have piecewise function J(t,m).
function [j] = functionJ (t,m)
model = @(t) -(0.0008.*m./(-0.01-0.07.*exp(-2.*t))-0.000112.*m.*t.*exp(-2.*t)./(-0.01-0.07.*exp(-2.*t)).^2).*exp(0.0008.*m.*t./(-0.01-0.07.*exp(-2.*t)));
j = (t<20).*((1/20)*integral(model,0,t)) + (t>=20).*((1/20)*integral(model,(t-20),t)); end
Now I need another function R(x,m) to integrate J in respect to t from 0 to x I'm still new with Matlab. Could you please give me hint how to do that?
Answers (2)
Youssef Khmou
on 6 May 2013
hi, try
doc quad
doc trapz
Andrei Bobrov
on 6 May 2013
Edited: Andrei Bobrov
on 7 May 2013
function J = R(f,x,m)
t = x >= 20;
p = zeros(numel(x),1);
p(t) = x(t) - 20;
J = arrayfun(@(x,y)integral(@(t)f(t,m),y,x),x,p);
eg use:
f = @(t,m) -(0.0008.*m./(-0.01-0.07.*exp(-2.*t))-0.000112.*m.*t.*exp(-2.*t)./...
(-0.01-0.07.*exp(-2.*t)).^2).*exp(0.0008.*m.*t./(-0.01-0.07.*exp(-2.*t)));
m = 1;
x = randi(40,20,1);%eg
out = R(f,x,m)
OR
function J = R(x,m)
t = x >= 20;
p = zeros(numel(x),1);
p(t) = x(t) - 20;
f = @(t,m) -(0.0008.*m./(-0.01-0.07.*exp(-2.*t))-0.000112.*m.*t.*exp(-2.*t)./...
(-0.01-0.07.*exp(-2.*t)).^2).*exp(0.0008.*m.*t./(-0.01-0.07.*exp(-2.*t)));
J = arrayfun(@(x,y)integral(@(t)f(t,m),y,x),x,p);
ADD
function J = pwint(x,m)
f = @(t,m) -(0.0008.*m./(-0.01-0.07.*exp(-2.*t))-0.000112.*m.*t.*exp(-2.*t)./...
(-0.01-0.07.*exp(-2.*t)).^2).*exp(0.0008.*m.*t./(-0.01-0.07.*exp(-2.*t)));
t = x >= 20;
p = zeros(numel(x),1);
p(t) = x(t) - 20;
J = arrayfun(@(x,y)integral(@(t)f(t,m),y,x),x,p);
eg use:
R = @(x,m)arrayfun(@(t)integral(@(x)pwint(x,m),0,t,'ArrayValued',true),x);
m = 1;
out = R([30,15],m);
Categories
Find more on Numerical Integration and Differentiation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!