Numeric integration with two variable

조회 수: 2(최근 30일)
Özgür Alaydin
Özgür Alaydin 2021년 11월 23일
댓글: Özgür Alaydin 2021년 11월 24일
Hello all ,
I have a function which depends on two variables z and t. z is a vector. I want to integrate the function over t but i need to find a matrix dependent to the z value. For this i wrote the code given below. But i could not get answer. I am getting either 0 matrix or single value as a result of integration. How can i integrate the function only for t and evalute the date for z values?
I tried trapz and integrate function but i failed. Please help
Vb=0.228;
dz = 2e-11;
Ltot = 20e-9;
z=-Ltot/2:dz:Ltot/2;
L = 10e-9;
a=2e-9;
alfa = 10e-9; omega=1e-21; t0=0; ts=2*pi/omega; t=(t0:(ts-t0)/(length(z)-1):ts);
V0 = abs(Vb./pi.*(heaviside(alfa-z-L/2) + heaviside(alfa+z-L/2)));
z = @(z,t) (z + alfa*sin(omega*t));
V0 = integral(V0,t0,ts) or % V0 = trapz(V0,[t0,ts]);
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use brackets instead of parentheses.
%%%% i need a z dependent V0 value %%%%%%%%%%%%%

답변(2개)

Walter Roberson
Walter Roberson 2021년 11월 23일
The first parameter to integral() must be a function handle. You are passing in V0 as the first parameter, but V0 appears to be a vector of values.
The second parameter to integral() must be a scalar numeric value, which is indeed true for the t0 you pass in.
The third parameter to integral() must be a scalar numeric value, which is indeed true for the ts you pass in.
You construct a function handle, z, but you do not do anything with it.
When you have a numeric function of several variables, it is not possible to integrate it with respect to fewer variables than it is defined over.
If you were using symbolic functions, then it would be possible to integrate it with respect to fewer variables.
When you have two independent variables, and you want to do numeric integration over a grid of values defined by the independent variables, then you should evaluate the function over the grid of values, and then use trapz() one or more times.
For example,
Vb=0.228;
dz = 2e-11;
Ltot = 20e-9;
z=-Ltot/2:dz:Ltot/2;
L = 10e-9;
a=2e-9;
alfa = 10e-9; omega=1e-21; t0=0; ts=2*pi/omega; t=(t0:(ts-t0)/(length(z)-1):ts);
Z = @(z,t) (z + alfa*sin(omega*t));
G = Z(z.', t);
surf(z, t, G, 'edgecolor', 'none')
size(G)
ans = 1×2
1001 1001
volume = trapz(z, trapz(t, G, 2), 1)
volume = 1.6371e-11
You should double-check the order of trapz() and dimensions. If you were to make your t and z different lengths, then it would be easier to do, as then you would have the cross-checking of array sizes as a guide.
  댓글 수: 1
Özgür Alaydin
Özgür Alaydin 2021년 11월 24일
If i run the code as you suggest, it is working but when i insert V0 value it is not and i am getting error: Operator '-' is not supported for operands of type 'function_handle'.
I need V1 value depending on z after inserting V0 into the equation.

댓글을 달려면 로그인하십시오.


VBBV
VBBV 2021년 11월 23일
Vb=0.228;
dz = 2e-11;
Ltot = 20e-9;
z=-Ltot/2:dz:Ltot/2;
size(z)
ans = 1×2
1 1001
L = 10e-9;
a=2e-9;
alfa = 10e-9;
omega=1e-21;
t0=0;
ts=2*pi/omega ;
t=(t0:(ts-t0)/(length(z)-1):ts);
z = @(z,t) (z + alfa*sin(omega*t));
V0 = @(z) abs(Vb./pi.*(heaviside(alfa-z-L/2) + heaviside(alfa+z-L/2))); % use this line after z
V1 = integral(V0,t0,ts) % V0 = trapz(V0,[t0,ts]);
V1 = 4.5600e+20
Use the z function before integrating V0 between the specified limits
  댓글 수: 1
Özgür Alaydin
Özgür Alaydin 2021년 11월 24일
When i correct and run the code, i am getting single value for V1. But i need to find it depending on the z-values. NOT SINGLE value. As you found and wrote beneath the code 4.56e+20, i don not want that.
So that i need to find a V1 for each value of z.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by