이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
How apply trapz on the double definite integral
조회 수: 6 (최근 30일)
이전 댓글 표시
SHAHID
2024년 5월 6일
F= int from 0 to x int from -1 to 1 e^wi(z-li*s)ds dz.
The value of x belongs to [0,1], l1=l2=3nm, kindly tell how to solve it by using trapz command.
채택된 답변
Star Strider
2024년 5월 6일
You have not provided enough information to write specific code.
As a general rule, to use double integration with trapz, create a 2D matrix from your functions, and then use trapz on each dimension of it.
.
댓글 수: 27
SHAHID
2024년 5월 6일
Actually wi is unknown and I want to use like this,
close all
clc
%format long
N=2;
D=7.5; % 1st value for D in nm
r1=3;r2=3; % the value of radius in nm
lB= 0.7; % Assume value of Bjerrum length in nm
z1 = [1, 10];
z2 = [-1, -10];
n10= 10^-3;n20=10^-3; %Bulk concentration in nm^-3
n0= 10^-3; % Value of concentration of ions in nm^-3
c1=1;c2=1;
% Define function
f=@(s,z) exp(wi(z-ri*s));
K=2*pi*lB*D^2*n0; % the value of K
% Create a grid of values for s and z
x=1;
z_values=linspace(-1,1,N); % for integral from -1 to 1
s_values=linspace(0,x,N);% value of x on domain D from 0 to 1
[s_grid, z_grid] = meshgrid(s_values, z_values);
% Evaluate the function f(s,z) on the grid
y = f(s_grid, z_grid);
% Compute the double integral using trapz
Li = trapz(s_values, trapz(z_values, y,1),2);
Display(Li)
its showed error in wi because its unknown and I will find it later.
Star Strider
2024년 5월 6일
I doubt that trapz will work with this problem, because to use it, ‘wi’ must be defined first. If you have a value for the integral and you want to estimate ‘wi’, use the integral2 function with ‘f’ along with an appropriate optimsation functions such as fminsearch or fsolve.
SHAHID
2024년 5월 6일
yes first I wrote ininitial code then I will use fmincon for finding wi, but can you tell me I can proceed next by using this code or not?
Star Strider
2024년 5월 6일
Probably something like this —
ri = rand % 'ri' Not Currently Defined
ri = 0.6353
z1 = [1, 10];
z2 = [-1, -10];
f=@(s,z,wi) exp(wi.*(z-ri.*s));
intval = 42; % Value Of Integral
intfcn = @(wi) integral2(@(s,z)f(s,z,wi), z1(1), z1(2), z2(1), z2(2))
intfcn = function_handle with value:
@(wi)integral2(@(s,z)f(s,z,wi),z1(1),z1(2),z2(1),z2(2))
wi_est = fsolve(@(wi) norm(intval - intfcn(wi)), rand)
No solution found.
fsolve stopped because the problem appears regular as measured by the gradient,
but the vector of function values is not near zero as measured by the
value of the function tolerance.
wi_est = 8.9652
fprintf('\nEstimated value for ‘wi’ is %.6f\n', wi_est)
Estimated value for ‘wi’ is 8.965215
This is the approach I would use for this sort of problem, assuming that I understand correctly what you want to do. Use the correct scalar values, and be certain that the integration limits are correct.
I do not understand your code, or what importance ‘K’ has, since it is not used.
.
SHAHID
2024년 5월 6일
my F=K<e summation i to N ci Li , e summation i to N ci Li -2 sigma>+ summation i to N e ci <e^wi,1>=0
and constraint is EN(w)= 2 sigma+ summation i to N e ci zi <e^wi,1>=0.
I want to solve this two function by fmincon(F_k(w_k)), EN_K(w_k)), I need code for it. Thanks for your response and I will wait for your response.
Star Strider
2024년 5월 6일
I have no idea what that means.
You need to write the code. I may be able to help you get it to produce teh correct results.
SHAHID
2024년 5월 7일
yes sure I need code I am thankful to you if you can help for writing code. Grazie
Star Strider
2024년 5월 7일
I will not write it for you.
I will help you get yours to run after you write it if you need help with it. You will also need to explain what it does, how it works, and what you want it to do.
SHAHID
2024년 5월 7일
편집: Sam Chak
2024년 5월 7일
yes sure thanks for your suggestion. I wrote code and its not working.
close all
clc
N=2;
D=7.5; % 1st value for D in nm
%r1=3;r2=3; % the value of radius in nm
lB= 0.7; % Assume value of Bjerrum length in nm
z1 = [1, 10];
z2 = [-1, -10];
n10= 1e-3;n20=1e-3; %Bulk concentration in nm^-3
n0= 1e-3; % Value of concentration of ions in nm^-3
c1=1;c2=1;
%x=[0,1];
% Define function
function f = myFunction(s, z)
f = exp(wi * (z - ri * s));
end
r1=3;r2=3; % the value of radius in nm
K=2*pi*lB*D^2*n0; % the value of K
% Create a grid of values for s and z
z_values=linspace(-1,1,N); % for integral from -1 to 1
s_values=linspace(0,x,N);% value of x on domain D from 0 to 1
Unrecognized function or variable 'x'.
[s_grid, z_grid] = meshgrid(s_values, z_values);
% Evaluate the function f(s,z) on the grid
y = myFunction(s_grid, z_grid);
% Compute the double integral using trapz
Li = trapz(s_values, trapz(z_values, y,1),2);
disp('The value of Li is:');
disp(Li )
%Define function F(w)
% Compute the first term: K <e sum from i to N ci Li, e sum from i to N ci Li>
%function term1= myfunc(wi)
term1 = K .*e.^2*sum(exp(sum(ci.*Li)) .* exp(sum(ci.*Li)));
%end
% Compute the second term: K <e sum from i to N ci Li, -2 sigma>
term2 = K .*e.* sum(exp(sum(ci.*Li)) * (-2*sigma));
% Compute the third term: summation i to N e ci <e^wi, wi>
term3 = e.*sum(zi.*ci .* exp(wi));
% Compute the final result: F(w) = term1 + term2 + term3
F =@(w) term1 + term2 + term3;
%Define constraint
% Compute the fourth term: summation i to N zi * ci * <e^wi, 1>
term4 = e.*sum(zi .* (ci .* exp(wi)));
EN=@(w) 2*sigma+ term4;
fmincon(F(w),EN(w))
Star Strider
2024년 5월 7일
First, running it, this line:
s_values=linspace(0,x,N);% value of x on domain D from 0 to 1
throws the error: Unrecongnized function or variable 'x'. because you have not previously defined ‘x’. The solution is to define ‘x’ before you use it. Also nmote thatthe value of ‘N’ is 2. That means that ‘s_values’ will consist of 0 and ‘x’, for example —
N=2;
x = rand
x = 0.0205
s_values=linspace(0,x,N);% value of x on domain D from 0 to 1
s_values
s_values = 1x2
0 0.0205
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Second, this function:
function f = myFunction(s, z)
f = exp(wi * (z - ri * s));
end
will throw errors when you try to use it because unlike anonymous functions, it will not pick up the values for ‘wi’ or ‘ri’ from the workspace. You must define them in the argument lilst, and provide them in the workspace before using them or calling the function that uses them.
There are several other errors that I see later in the code. We will address those when they become problams.
.
SHAHID
2024년 5월 7일
Thanks for your response. Dear Sir, x=[0,1] and we take nodes from it. wi is our vector which I want to find at end of code the value of wi(x)= logn_i(x)-1. I am interested evaluate F for finding wi(x)?
Star Strider
2024년 5월 7일
I am lost.
If ‘s_values’ is supposed to go from (0,1) with length ‘N’ then ‘N’ needs to be the number of values you want it to return. For a linspace argument, ‘x’ must be a scalar, not a (1x2) vector.
In the ‘F’ function expression, ‘term1’, ‘term2’, and ‘term3’ have to be anonymous functions of their respective arguments (or at least ‘w’ if all the other variables are present in the workspace) in order for ‘F’ to be evaluated. Then ‘F’ must be:
F =@(w) term1(w) + term2(w) + term3(w);
or whatever is appropriate for it.
Torsten
2024년 5월 7일
We can neither understand your problem from your text description nor from your code.
If this is a language problem, use the google translator. If not, we cannot help.
SHAHID
2024년 5월 8일
Dear Sir, I wrote my original problem which is given as,
F(w)= <
,
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1689592/image.png)
where
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1689597/image.png)
The equality constraint is EN(w)=
.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1689602/image.png)
Data for test:
D=7.5,
,
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1689617/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1689607/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1689612/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1689617/image.png)
Sir these are the known values and my problem. I want to use fmincon for F(w) and EN(w). wi is unknown and it will find after fmincon by fsolve command. I also share my modified code and I hope now you understand problem. I will wait your kind response.
close all
clc
N = 2;
D = 7.5; % 1st value for D in nm
lB = 0.7; % Assume value of Bjerrum length in nm
z1 = [1, 10];
z2 = [-1, -10];
n10 = 1e-3;
n20 = 1e-3; % Bulk concentration in nm^-3
n0 = 1e-3; % Value of concentration of ions in nm^-3
c1 = 1;
c2 = 1;
% Define function
function f = myFunction(s, z)
f = exp(wi * (z - ri * s));
end
% Define variables
r1 = 3; r2 = 3; % the value of radius in nm
K = 2 * pi * lB * D^2 * n0; % the value of K
% Create a grid of values for s and z
z_values = linspace(-1, 1, N); % for integral from -1 to 1
s_values = linspace(0, 1, N); % value of x on domain D from 0 to 1
[s_grid, z_grid] = meshgrid(s_values, z_values);
% Evaluate the function f(s,z) on the grid
y = myFunction(s_grid, z_grid);
% Compute the double integral using trapz
Li = trapz(s_values, trapz(z_values, y, 1), 2);
disp('The value of Li is:');
disp(Li )
% Define function F(w)
function F_val = F(wi)
% Compute the first term: K <e sum from i to N ci Li, e sum from i to N ci Li>
term1 = K .* e.^2 * sum(exp(sum(ci .* Li)) .* exp(sum(ci .* Li)));
% Compute the second term: K <e sum from i to N ci Li, -2 sigma>
term2 = K .* e .* sum(exp(sum(ci .* Li)) * (-2 * sigma));
% Compute the third term: summation i to N e ci <e^wi, wi>
term3 = e .* sum(zi .* ci .* exp(wi));
% Compute the final result: F(w) = term1 + term2 + term3
F_val = term1 + term2 + term3;
end
% Define constraint function EN(w)
function EN_val = EN(wi)
% Compute the fourth term: summation i to N zi * ci * <e^wi, 1>
term4 = e .* sum(zi .* (ci .* exp(wi)));
EN_val = 2 * sigma + term4;
end
% Use fmincon to minimize F(w) subject to EN(w)
w0 = zeros(size(z1));
options = optimoptions('fmincon', 'Display', 'iter');
[w_opt, fval] = fmincon(@F, w0, [], [], [], [], [], [], @EN, options);
disp('Optimal w values:');
disp(w_opt);
disp('Minimum value of F(w):');
disp(fval);
Torsten
2024년 5월 8일
편집: Torsten
2024년 5월 8일
Do the angled bracket mean "scalar product" ? How is the scalar product defined ? Because somehow, x must be integrated out if F is only a function of w.
What is e ? Is it exp(1) ?
What does wi(x) mean ? Is wi a function of x or does it mean wi * x ? Same in the function expression wi(z-ri*s).
Are ci and ri N given numbers, respectively ?
What do you mean by e^wi in C([ri,1-ri]) ?
Despite the "explanation", your problem is still a riddle for me.
SHAHID
2024년 5월 8일
Dear Sir,
Angled bracket is inner product of vectors which defines as,
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1689847/image.png)
The value of e is 2.73333. wi is function of x and this is our unknown which will evaluate at last. wi(x) is changed into wi(z-ri*s) when we apply the definition of L_i which I mentioned before.
When N=2 then ci=1 for i=1,2.
ri is the radius of ions from continuous space C[ri, 1-ri]. If you need any information then ask. Thanks for your suggestions and I will wait your response.
Torsten
2024년 5월 8일
So wi(z-ri*s) means that the function wi is to be evaluated at z-ri*s ?
If the wi are functions, not vectors of numbers, you cannot use "fmincon" to determine them.
Torsten
2024년 5월 9일
You must evaluate w for different input arguments (e.g. in the integration). That's not possible if wi is a vector.
Torsten
2024년 5월 9일
You can consider $wi(x)=log (n_i(x)-1).
Now other functions are solution variables for fmincon, namely n_i(x) ? It does not work to solve for functions using "fmincon" - at least not in the way you try it in the code.
You should try to understand the mathematical problem better - I think we both don't understand it completely until now.
John
2024년 5월 9일
To apply the trapz function for double definite integration in MATLAB, you need to perform the integration in two steps.
Define the integrand function:
- Create a function that represents the integrand e^(wi(z-li*s)).
- The function should take the variables z and s as inputs and return the value of the integrand.
----------
Perform the inner integration:
- Create a vector of points for the variable s over the interval [-1, 1].
- For each value of z, evaluate the inner integral using trapz by integrating the integrand function with respect to s.
- Store the results of the inner integration in a vector.
-----
Perform the outer integration:
- Create a vector of points for the variable z over the interval [0, x].
- Use trapz to integrate the results of the inner integration with respect to z.
SHAHID
2024년 5월 13일
Thanks Sir for your response. I am sharing code as you said kindly check and modified if possible for you. I am thankful to your for this act of kind.
close all
clc
N = 2;
D = 7.5; % 1st value for D in nm
lB = 0.7; % Assume value of Bjerrum length in nm
z1 = [1, 10];
z2 = [-1, -10];
n10 = 1e-3;
n20 = 1e-3; % Bulk concentration in nm^-3
n0 = 1e-3; % Value of concentration of ions in nm^-3
c1 = 1;
c2 = 1;
e=exp(1);
% Define function
function f = myFunction(s, z)
f = exp(wi.*(z - ri .* s));
end
% Define variables
r1 = 3; r2 = 3; % the value of radius in nm
K = 2 * pi * lB * D^2 * n0; % the value of K
sigma=13; %when a=1nm^2
% Create a grid of values for s and z
s_values = linspace(-1, 1, N); % for integral from -1 to 1
z_values = linspace(0, 1, N); % value of x on domain D from 0 to 1
[s_grid, z_grid] = meshgrid(s_values, z_values);
% Evaluate the function f(s,z) on the grid
y = myFunction(s_grid, z_grid);
% Compute the double integral using trapz
Li = trapz(s_values, trapz(z_values, y, 1), 2);
disp('The value of Li is:');
disp(Li )
% Define function F(w)
function F_val = F(wi)
% Compute the first term: K <e sum from i to N ci Li, e sum from i to N ci Li>
term1 = K .* e.^2 * sum(exp(sum(ci .* Li)) .* exp(sum(ci .* Li)));
% Compute the second term: K <e sum from i to N ci Li, -2 sigma>
term2 = K .* e .* sum(exp(sum(ci .* Li)) * (-2 * sigma));
% Compute the third term: summation i to N e ci <e^wi, wi>
term3 = e .* sum(zi .* ci .* exp(wi));
% Compute the final result: F(w) = term1 + term2 + term3
F_val = term1 + term2 + term3;
end
% Define constraint function EN(w)
function EN_val = EN(wi)
% Compute the fourth term: summation i to N zi * ci * <e^wi, 1>
term4 = e .* sum(zi .* (ci .* exp(wi)));
EN_val = 2 * sigma + term4;
end
% Use fmincon to minimize F(w) subject to EN(w)
[w_opt, F_val] = fmincon(@F, w0, [], [], [], [], [], [], @EN, options);
disp('Minimum value of F(w):');
disp(F_val);
SHAHID
2024년 5월 13일
close all
clc
% Define variables
wi = 1; % Define wi value
ri = 0.5; % Define ri value
m=7; %Nodes
% Preallocate array to store Li values
Li_values = zeros(1, m);
for i = 1:m
% Create a grid of values for s and z
%x=linspace(0,1,7);
s_values = linspace(-1, 1, i); % Adjust grid size
z_values = linspace(0, 1, i); % Adjust grid size
% Compute Li for each node
[s_grid, z_grid] = meshgrid(s_values, z_values);
f_values = myFunction(s_grid, z_grid, wi, ri); % Pass wi and ri to the function
Li = trapz(z_values, trapz(s_values, f_values, 1), 2);
Li_values(i) = Li;
end
% Display Li values
disp('The values of Li are:');
disp(Li_values);
% Define function outside the script or in a separate file
function f = myFunction(s, z, wi, ri)
f = exp(wi*(z - ri * s));
end
Dear Sir, I wrote just first part of code for defining L_i.
추가 답변 (0개)
참고 항목
태그
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)