How apply trapz on the double definite integral

9 views (last 30 days)
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
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
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
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.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!