MATLAB: Do I get error for integral

MATLABmatlab integral

Hello,
I'm quite a matlab dummy and it would be nice, if you could help me with this.
When integrating the following function I get the error "Array indices must be positive integers or logical values.". I think it's about the "f(x)-u_equ(x))" term, which is negative. But as the formula is like this I don't know what to do. Sorry in advance if the question/solution is trivial.
clear all;
n=1;
t1 = 14;
t2 = 25;
len = 20; %length of model
initemp = 10; %initial temperature
steps = 20;
u_equ=zeros(1,steps);
f=zeros(1,steps);
% equilibrium temperature
for x=1:steps
u_equ(x) = t1+((t2-t1)/len).*x;
f(x) = initemp;
end
fun = @(x) (f(x)-u_equ(x)).*sin((n.*pi.*x)/len); %function to integrate
intgr = integral(fun,0,len)

Best Answer

  • Note that ‘f’ and ‘u_equ’ are vectors, not functions.
    So ‘fun’ is now:
    fun = @(x) (f-u_equ).*sin((n.*pi.*x)/len); %function to integrate
    and ‘intgr’ adds ‘ArrayValued’ to its argument list:
    intgr = integral(fun,0,len,'ArrayValued',1)
    producing:
    intgr =
    -57.9324 -64.9352 -71.9380 -78.9409 -85.9437 -92.9465 -99.9493 -106.9521 -113.9549 -120.9578 -127.9606 -134.9634 -141.9662 -148.9690 -155.9718 -162.9747 -169.9775 -176.9803 -183.9831 -190.9859