error using integral function
1 view (last 30 days)
Show older comments
clc
clear all
close all
a=7.2*10^-6;
Gm=2.5*10^-2;
b=4.7;
sp=2.75*10^-5;
yon=6*10^-2;
son=4.5*10^-1;
B=10^-4;
A=10^-10;
soff=1.3*10^-2;
beta=500;
yoff=1.3*10^-2;
t=0:0.001:0.007;
vm=0.1:0.001:0.107;
gp=tanh(vm);
f=145;
v=sawtooth(2*pi*f*t);
y=(yon^2*(Gm-a*exp(b*sqrt(gp))).*v.^2)/(2*sp);
i=v.*(y*Gm+(1-y)*a.*(Gm-a*exp(b*sqrt(gp))));
Q145=integral(@(t) i,0,0.5);
error is comin like
Error using integralCalc/finalInputChecks (line 526)
Output of the function must be the same size as the input. If FUN is an array-valued integrand, set the 'ArrayValued'
option to true.
Error in integralCalc/iterateScalarValued (line 315)
finalInputChecks(x,fx);
Error in integralCalc/vadapt (line 132)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 75)
[q,errbnd] = vadapt(@AtoBInvTransform,interval);
Error in integral (line 88)
Q = integralCalc(fun,a,b,opstruct);
Error in Untitled7 (line 22)
Q145=integral(@(t) i,0,0.5);
>>
0 Comments
Answers (2)
Jan
on 29 Apr 2021
Edited: Jan
on 29 Apr 2021
You do not integrate a function, but a vector:
i = v.*(y*Gm+(1-y)*a.*(Gm-a*exp(b*sqrt(gp))));
Q145=integral(@(t) i,0,0.5);
i is not a function depending on the input t, but a vector. Use trapz for integrating vectors.
By the way, the old brute clearing header "clc; clear all; close all" is a waste of time only. clear all removes all loaded frunction from the memory and reloading them from the slow disk takes time without any benefit. Use functions to keep the workspace clean.
Run time is not a point of your problem. But for real application prefer the cheap constand 7.2e-6 instead of the expensive power operation 7.2*10^-6.
3 Comments
Jan
on 29 Apr 2021
t = 0:0.1:1;
y = sin(t);
Now y is a constant, which does not depend on t anymore. See:
t = 27;
y % Of course this does not change its value
A function depending on a variable looks like this:
f = @(t) sin(t)
% Test it:
f(1)
f(2)
Star Strider
on 29 Apr 2021
‘but in directly i is depending on t because v is depending on t and y is depending on v’
True, however integral has no way of knowing that unless you tell it!
Try this —
format long g
a=7.2*10^-6;
Gm=2.5*10^-2;
b=4.7;
sp=2.75*10^-5;
yon=6*10^-2;
son=4.5*10^-1;
B=10^-4;
A=10^-10;
soff=1.3*10^-2;
beta=500;
yoff=1.3*10^-2;
% t=0:0.001:0.007;
vm=0.1:0.001:0.107;
gp=tanh(vm);
f=145;
v = @(t) sawtooth(2*pi*f*t);
y = @(t) (yon^2*(Gm-a*exp(b*sqrt(gp))).*v(t).^2)/(2*sp);
i = @(t) v(t).*(y(t)*Gm+(1-y(t))*a.*(Gm-a*exp(b*sqrt(gp))));
Q145=integral(i,0,0.5, 'ArrayValued',1)
figure
plot(vm, Q145)
grid
I will let you figure out how it wworks!
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!