You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How apply trapz on the double definite integral
9 views (last 30 days)
Show older comments
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.
Accepted Answer
Star Strider
on 6 May 2024
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 Comments
SHAHID
on 6 May 2024
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
on 6 May 2024
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
on 6 May 2024
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
on 6 May 2024
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
on 6 May 2024
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
on 6 May 2024
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
on 7 May 2024
yes sure I need code I am thankful to you if you can help for writing code. Grazie
Star Strider
on 7 May 2024
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
on 7 May 2024
Edited: Sam Chak
on 7 May 2024
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
on 7 May 2024
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
on 7 May 2024
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
on 7 May 2024
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
on 7 May 2024
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
on 8 May 2024
Dear Sir, I wrote my original problem which is given as,
F(w)= <,
where
The equality constraint is EN(w)=.
Data for test:
D=7.5, ,
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
on 8 May 2024
Edited: Torsten
on 8 May 2024
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
on 8 May 2024
Dear Sir,
Angled bracket is inner product of vectors which defines as,
.
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
on 8 May 2024
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
on 9 May 2024
You must evaluate w for different input arguments (e.g. in the integration). That's not possible if wi is a vector.
Torsten
on 9 May 2024
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
on 9 May 2024
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
on 13 May 2024
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
on 13 May 2024
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.
More Answers (0)
See Also
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)