error using integral function

11 views (last 30 days)
nune pratyusha
nune pratyusha on 29 Apr 2021
Commented: Jan on 29 Apr 2021
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);
>>

Answers (2)

Jan
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
nune pratyusha
nune pratyusha on 29 Apr 2021
but in directly i is depending on t because v is depending on t and y is depending on v
Jan
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)

Sign in to comment.


Star Strider
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)
Q145 = 1×8
-3.53002158342886e-05 -3.52998846582496e-05 -3.52995527482942e-05 -3.52992200995947e-05 -3.52988867074367e-05 -3.52985525672159e-05 -3.52982176744341e-05 -3.5297882024696e-05
figure
plot(vm, Q145)
grid
I will let you figure out how it wworks!

Tags

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!