Trapezoidal rule for integral function

7 ビュー (過去 30 日間)
lulu
lulu 2022 年 9 月 11 日
コメント済み: Torsten 2022 年 9 月 12 日
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

採用された回答

Walter Roberson
Walter Roberson 2022 年 9 月 11 日
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 件のコメント
Walter Roberson
Walter Roberson 2022 年 9 月 12 日
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.
Torsten
Torsten 2022 年 9 月 12 日
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 件)

カテゴリ

Help Center および File ExchangeNumerical 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