integral parameterization in MATLAB [duplicate] - matlab

This question already has answers here:
Using MATLAB integral with anonymous functions
(1 answer)
How do I properly setup numerical integration in MATLAB?
(2 answers)
Closed 6 months ago.
I want to use integral function which is parameterized. but it gives an error. the upper limit in integral function is a parameter.
my code is here:
f1=#(t) (25.46/t)+0.01519
antf1=integral(f1,298.15,t)
but, for example, the following code does not give any error:
f2=#(t) 25.46*(1.519*t/100)
antf2=integral(f2,298.15,t)
thank you all.
UPDATE 1
the complete code is here:
clc
clear
i=1;
j=1;
a=1.4979e-06;
b=1.9829e-05;
r=0.00008314;
for t=173.15:10:423.15
tr=t/154.6;
for p=1:1:100
pr=p/50.45;
aa=( 1.4084*(1-(t/154.6)^0.5 ))^2;
eq=[p -((3*b*p)+(r*t)) -((r*2*t*b)-(aa*a*b)+(3*p*(b^2))) (((b^3)*p)-(aa*a*b)+(r*t*(b^2)))];
rr=roots(eq);
rq=rr(1);
v(i,j)=rq;
bb=(r*154.6/50.45)*((0.083-(0.422/(tr^1.6)))+(0.022*(0.139-(0.172/(tr^4.2)))));
zv(i,j)=1+((pr/tr)*(bb*50.45/(r*154.6)));
%z(i,j)=(v(i,j)*p)/(r*t);
fun1= #(t) 25.46+(1.519*t/100) ;%-(0.7151*(t^2)/100000)+(1.311*(t^3)/1000000000);
fun2= #(p) ((p/(t*r))*(r*154.6/50.45)*((0.6752*(154.6^1.6)/(t^0.6))+((0.7224*(154.6^4.2)/(t^3.2)))))-(p*bb/(r*(t^2)));
infun1=integral(fun1,298.15,t);
infun2=integral(fun2,1,p);
h(i,j)= infun1 - (r*(t^2))*infun2;
f1= #(t) (25.46/t)+0.01519 ;
f2= #(p) ((1/(t*r))*(r*154.6/50.45)*((0.6752*(154.6^1.6)/(t^0.6))+((0.7224*(154.6^4.2)/(t^3.2)))))-(1*bb/(r*(t^2)));
f3= #(p) (p*t/(50.45*154.6))*(bb*50.45/(r*154.6));
anf3=integral(f1,298.15,t); %%%%%%%%%%% LINE 28 %%%%%%%%%%%
anf4=integral(f2,1,p);
anf5=integral(f3,1,p);
s(i,j)=anf3-(r*log(p/1))+(r*((-1)*t*anf4)-anf5);
i=i+1;
end
i=1;
j=j+1;
end
v;
%z;
zv;
h;
s;
the error message is shown in the following:
Error using /
Matrix dimensions must agree.
Error in code>#(t)(25.46/t)+0.01519
Error in integralCalc/iterateScalarValued (line 314)
fx = FUN(t);
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 code (line 28)
anf3=integral(f1,298.15,t);

Related

Sequential numerical integration in MATLAB

I am trying to write the program in MATLAB to perform sequential integration. First I define the first integral n_array_exact{1}() and save it as function handle in the array. This first integral becomes a function of x only after the integration. As a next step I use this function to calculate next double integral (call it n_array_exact{2}(x)) w.r.t mu1 and s. Before substituting n_array_exact{1}(x) to calculate n_array_exact{2}(x), I replace x in n_array_exact{1}(x) to a function called xp(x, mu1,s) instead.
% Ordinary integration
n_array_exact{1} = #(x) integral(#(mu) exp((x.*mu)), -1,1)/2;
xp2= #(x,mu1,s) sqrt(x*x +s*s +2*x*mu1*s);
for i=2:3
n_array_exact{i} = #(x) integral2(#(mu1,s) exp(-s.*mu1.*x).*n_array_exact{1}(xp2(x,mu1,s)), 0,1,-1,1);
end
n_array_exact{2}(0.05)
%n_array_exact{3}(0.05)
%n_array_exact{4}(0.05)
After performing the integration and trying to evaluate n_array_exact{2}0.05) I got the following errors:
Error using .*
Matrix dimensions must agree.
Error in #(mu)exp((x.*mu))
Error in integralCalc/iterateScalarValued (line 315)
fx = FUN(t);
Error in integralCalc/vadapt (line 133)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 76)
[q,errbnd] = vadapt(#AtoBInvTransform,interval);
Error in integral (line 88)
Q = integralCalc(fun,a,b,opstruct);
Error in #(x)integral(#(mu)exp((x.*mu)),-1,1)/2
Error in #(mu1,s)exp(-s.*mu1.*x).*n_array_exact{1}(xp2(x,mu1,s))
Error in integral2Calc>integral2t/tensor (line 229)
Z = FUN(X,Y); NFE = NFE + 1;
Error in integral2Calc>integral2t (line 56)
[Qsub,esub] = tensor(thetaL,thetaR,phiB,phiT);
Error in integral2Calc (line 10)
[q,errbnd] = integral2t(fun,xmin,xmax,ymin,ymax,optionstruct);
Error in integral2 (line 106)
Q = integral2Calc(fun,xmin,xmax,yminfun,ymaxfun,opstruct);
Error in #(x)integral2(#(mu1,s)exp(-s.*mu1.*x).*n_array_exact{1}(xp2(x,mu1,s)),0,1,-1,1)
Error in test (line 10)
n_array_exact{2}(0.05)

How to integrate a complicated function?

I have been trying to figure out this integral for some time, but have come up short. I have tried doing a symbolic integration, but I get it shot back out at me, so I am assuming there is not solution that way. I have resolved to solve it with a definite integral, but still keep getting errors:
clear
clc
x = 1;
y = 1;
z = 1;
R = 2;
b =#(theta) y.*cos(theta)/((x-R.*cos(theta)).^2+y.^2+(z -
R.*sin(theta)).^2).^(3/2)
integral(b,1,2)
My current error is:
Error using integralCalc/finalInputChecks (line 515)
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);
Any help would be very appreciated!
You need to change the division sign in the first term from / to ./ to ensure that you are doing element-wise division and not matrix right division:
b = #(theta) y.*cos(theta)./((x-R.*cos(theta)).^2+y.^2+(z - ...
R.*sin(theta)).^2).^(3/2);
integral(b,1,2)
ans =
0.055781612354862

multiple integrals in the same variable in MATLAB

I need to evaluate the following integral in MATLAB (numerically):
I already tried various things but I can't figure out how to solve this! Following is my last try:
Fdx = #(x) integral(#(y)1./(1+sqrt(y.^2))*(1-pi^2),0,x);
dFdx(1)
F = 8 * integral(dFdx,0,10)
As a result MATLAB gives me this error message:
Error using integral (line 85)
A and B must be floating-point scalars.
Error in #(x)integral(#(y)1./(1+sqrt(y.^2))*(1-pi^2),0,x)
Error in integralCalc/iterateScalarValued (line 314)
fx = FUN(t);
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 test (line 7)
F=8 * integral(dFdx,0,10)
Try using integral2 instead. See example 2 in the documentation:
http://www.mathworks.com/help/matlab/ref/integral2.html
Hope it helps.

Numerical integration, dimensions do not agree despite use of .^ and .*

I have an issue with Matlab integration. It tells me that there is an error about dimensions. However they do agree and operations are properly done using vectorized operators (.^ .* etc...). It is simple code yet I am stuck
A = 1:10;
B = 1:10;
K_fun = #(x) (x ./ sqrt((x + A.^2 ) .* (x + B.^2) .* (x + B.^2)) );
K = integral( K_fun, 0,Inf );
and here the error message in the command window:
Error using +
Matrix dimensions must agree.
Error in #(x)(x./sqrt((x+A.^2).*(x+B.^2).*(x+B.^2)))
Error in integralCalc/iterateScalarValued (line 314)
fx = FUN(t);
Error in integralCalc/vadapt (line 133)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 84)
[q,errbnd] = vadapt(#AToInfInvTransform,interval);
Error in integral (line 89)
Q = integralCalc(fun,a,b,opstruct);
Error in PROVA_New_Drag3 (line 21)
K = integral( K_fun , 0 , Inf);
Thank you in advance
It evaluates both your integration limits in a vector, so effectively your xin the function is [0 Inf], which is why the lengths don't agree.
You can set the 'ArrayValued' flag to true in the integral call
K = integral( K_fun, 0,Inf,'ArrayValued', true );
to get it to evaluate them separately.
I got another warning about a singularity but this is likely because you're using 0, infinity and division so it's related more to your function than the integration call. It might help to to sprinkle some eps around in K_fun.
UPDATE: Please see Troy's explanation of the singularity warning and note that eps won't actually help with that.

Step functions/Heaviside Functions MATLAB error

So im trying to plot f(t) which is a piecewise function using heaviside and ezplot functions in MATLAB. Now im not very familiar with MATLAB at all. If anyone knows why im getting this error it would be helpful.
f = '12+(-2t+8)*heaviside(t-2)+(2t-12)*heaviside(t-6)'
f = 12+(-2t+8)*heaviside(t-2)+(2t-12)*heaviside(t-6)
ezplot(f)
Error using inlineeval (line 15) Error in inline expression ==> 12+(-2t+8).*heaviside(t-2)+(2t-12).*heaviside(t-6) Error: Unexpected MATLAB expression.
Error in inline/feval (line 34)
INLINE_OUT_ = inlineeval(INLINE_INPUTS_, INLINE_OBJ_.inputExpr, INLINE_OBJ_.expr);
Error in ezplotfeval (line 52)
z = feval(f,x(1));
Error in ezplot>ezplot1 (line 469)
[y, f, loopflag] = ezplotfeval(f, x);
Error in ezplot (line 145)
[hp, cax] = ezplot1(cax, f{1}, vars, labels, args{:});
You need to tell Matlab to multiply explicitly, use 2*t instead of 2t.
syms t real
f = 12+(-2*t+8)*heaviside(t-2)+(2*t-12)*heaviside(t-6)
ezplot(f,[0 10])