Trapezoidal rule for integral function

I need to apply trapezoidal rule for this Function and put it inside that tanh
: F=1-b*integral{K(s) v(x+s)}+d*integral{K(s) ur(x+s)} where K(s)=1/0.01*sqrt(2pi)*exp-s^2*/2*sigma^2 where a=0, b=1. i got error because of array. see the attached please

 采纳的回答

x=xl:dx:xr;
That is a numeric vector, that is not integers
K(x)=1/(0.05*sqrt(2*pi))*exp(-0.5*(x/0.05).^2);
y(x)= b*K(x)*v+d*K(x)*ur;
x is a numeric vector that is not integers. K is not defined at that point, so K(x) on the left side means that you are creating a variable and indexing it at those non-integer locations, which is an error.
You appear to trying to define a formula for K and y. In MATLAB, in order to define a formula using the syntax you are using, the indexing variable on the left needs to be a symbolic variable created with sym() or syms()
The non-symbolic way to define a formula in MATLAB is to use anonymous functions. That would look like
K = @(x) 1/(0.05*sqrt(2*pi))*exp(-0.5*(x/0.05).^2);
y = @(x) b*K(x)*v+d*K(x)*ur;

6 个评论

I see. Thank yoy for your comment. I did according to your advise but still has error regarding the integral.
How I can use the trapezoidal rule for the integral of y?. I tried as we can see in the attached file but it is not working.
Didn't you read Walter's response ?
K and y have to be defined as function handles:
K = @(x) 1/(0.05*sqrt(2*pi))*exp(-0.5*(x/0.05).^2);
y = @(x) b*K(x)*v+d*K(x)*ur;
not as
K(x)=1/(0.05*sqrt(2*pi))*exp(-0.5*(x/0.05).^2);
y(x)= b*K(x)*v+d*K(x)*ur;
Then you can evaluate them as
xnum=xl:dx:xr;
ynum = y(x);
I = trapz(xnum,ynum)
Thank you for replaying, I saw his responce but I got error when I applied trapezoidal rule there (xnum=xl:dx:xr;
ynum = y(x);
I = trapz(xnum,ynum));
>> trapezoidal
Error using trapz (line 66)
Point spacing must be a scalar specifying uniform spacing or a vector of x-coordinates for each data point.
Error in trapezoidal (line 62)
I = trapz(ynum,xnum);
ur=zeros(J+1,Nt);
ul=zeros(J+1,Nt);
vr=zeros(J+1,Nt);
vl=zeros(J+1,Nt);
v=zeros(J+1,Nt);
Those are all arrays of zeros.
%%%%%%%%%%%%%%%%%%%%%%%%
K = @(x)1./(0.05*sqrt(2*pi)).*exp(-0.5*(x/0.05).^2);
y = @(x)b*K(x)*v+d*K(x)*ur;
The anonymous function definition for y captures the all-zero v and ur and freezes those arrays into the definition of y. y will not use the current value of v and ur as v and ur are updated.
For scalar inputs, K will return a scalar. b and d are scalars. So y will be scalar * array + scalar * array and the result of that is going to be an array.
Question: why do you calculate K(x) twice? Why did you not define
y = @(x) K(x) * (b * v + d * ur)
and then since b and v and d and ur are constants relative to the function, why did you not do
bvdur = (b * v + d * ur);
y = @(x) K(x) * bvdur;
?
F = int(y,w);
y is a function handle, but w is vector of 100 elements. int() is symbolic integration, not numeric integration. But int() requires that the second parameter be the name of a symbolic variable to integrate over.
What was your intention for integrating the function passing in a vector ? Especially since you seem to be doing indefinite integration rather than definite integration.
Q=trapz(F,w);
w is your vector of independent values. trapz() expects the vector of independent values to be the first parameter, so like
Q = trapz(w, F);
You do not use K or y later in the code after you have changed ur and v... and with v and ur being intialized to all 0, (b * v + d * ur) is going to be all 0, and so your y is going to return an array of zeros.
According to the error message, your call to trapz is
I = trapz(ynum,xnum);
This is wrong. The input arguments must be reversed:
I = trapz(xnum,ynum);

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Numerical Integration and Differentiation 的更多信息

产品

版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by