error using integral function

2 ビュー (過去 30 日間)
nune pratyusha
nune pratyusha 2021 年 4 月 29 日
コメント済み: Jan 2021 年 4 月 29 日
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);
>>

回答 (2 件)

Jan
Jan 2021 年 4 月 29 日
編集済み: Jan 2021 年 4 月 29 日
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 件のコメント
Jan
Jan 2021 年 4 月 29 日
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
Star Strider 2021 年 4 月 29 日
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!

カテゴリ

Find more on Matrix Computations in Help Center and File Exchange

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by