I have this system of linear equations and i need to solve it for unknowns. how i can solve it in MATLAB? we have 8 unknowns and 8 equations.

$$
\begin{aligned}
& b_{0}^{l}=a_{0}^{l} A_{00}^{l}+\sum_{n=a_{n}}^{\infty} a_{n}^{l} A_{0} n^{\ell}+Z_{0}^{l} \\
& b_{m}^{l}=a_{0}^{l} A_{m 0}^{l}+\sum_{n=1}^{\infty} a_{n}^{l} A_{m n}^{l}+Z_{m}^{l} \\
& a_{0}^{l}=b_{0}^{l} B_{00}^{l}+\sum_{m=1}^{\infty} b_{m}{ }^{l} B_{0 m}^{l}+c_{0}^{l} C_{00}^{l}+\sum_{m=1}^{\infty} c_{m}^{l} C_{0 m}^{l}+Y_{0}^{l} \\
& a_{n}^{l}=b_{0}^{l} B_{n 0}^{l}+\sum_{m=1}^{\infty} b_{m}^{l} B_{n m}^{l}+c_{0}^{l} C_{n 0}^{l}+\sum_{m=1}^{\infty} c_{m}^{l} C_{n m}^{l}+Y_{n}^{l}
\end{aligned}
$$
and:
$$
\begin{aligned}
& c_{0}^{l}=d_{0}^{l} D_{00}^{\ell}+\sum_{n=1}^{\infty} d_{n}^{l} D_{0 n}^{l}+X_{0}^{l} \\
& c_{m}^{l}=d_{0}^{l} D_{m 0}^{\ell}+\sum_{n=1}^{\infty} d_{n}^{l} D_{m n}^{\ell}+X_{m}^{\ell} \\
& d_{0}^{\ell} = b_{0}^{l} E_{00}^{\ell}+\sum_{m=1}^{\infty} b_{m}^{\ell} E_{0 m}^{l}+c_{0}^{l} F_{00}^{l}+\sum_{m=1}^{\infty} c_{m}^{l} F_{0 m}^{l}+W_{0}^{l} \\
& d_{n}^{l}=b_{0}^{l} E_{n 0}^{l}+\sum_{m=1}^{\infty} b_{m}^{l} E_{n m}^{l}+c_{0}^{l} F_{n 0}^{l}+\sum_{m=1}^{\infty} c_{m}^{l} F_{n m}^{l}+W_{n}^{l}
\end{aligned}
$$
Unknowns are:-
$$
\begin{gathered}
\left(a_{0}^{l}, a_{n}^{l}\right) ;\left(b_{0}^{l}, b_{n}^{l}\right),\left(c_{0}^{l}, c_{n}^{l}\right) and \\
\left(d_{0}^{l}, d_{n}^{l}\right) .
\end{gathered}
$$
The above system of equations which is linear one is the latex code for these equations and i need to solve it for these unknowns. I am confused that how i can solved this system. how i can write these equations in matrix form to solved it for the unknowns? Help me.

7 Commenti

Hi Javeria,

Conventionally, we can use the equationsToMatrix() function to transform a system of linear equations to matrix form, A·x = b. But I see many sums of infinite series.

@Sam Chak yes we will turncate the sum to a definite number say n=1 upto N , m=1 upto M
@Sam Chak The other knonwn coefficients i.e A_00 etc they are given. i need to solve the system for the unknowns.
If the series are convergent, you should see that the sum of its terms approaches a finite value as the number of terms increases. Have you evaluated all the series using the symsum() command? For example...
syms k x
T = ((-1)^k)*4/(2*k + 1)
T = 
F = symsum(T, k, 0, Inf)
F = 
π
@Sam Chak we can defined the number for its truncation by taking a big number i.e
M=20;
N=50;
My question is that i need to solve this system of linear equations. so i need to write 1st the system in matrix i.e AX=B then i can solve by using X=A^{-1}B. But I am confused about that how to write this system in AX=B form.

Accedi per commentare.

 Risposta accettata

clc;
close all;
clear all;
tic;
N = 20; % Number of terms in region E and I
M= 50; % Number of term in region B
RE = 1.0; % Radius of external cylinder
ratios = [1.031,1.5, 2.0, 4.0]; % Ratios RE/RI you want to test
b= 2.0*RE; % The distance from seabed to the bottom of cylinder (h-d)=b
h = 4.0*RE; % Water depth.
g = 9.81; % Gravity acceleration
% Preallocate wave numbers k
nu =0.01; % nu
k=linspace(0,0,N);
% % Use the algorithm of Chamberlin & Porter
a2=nu*h;
p=a2*(1-4*(1-(1+a2)*exp(-2*a2))/(2*a2+sinh(2*a2)))^(-0.25); % approximation for k_0 (Eq. 19)
k(1)=p;
for i2=1:N-1
b2=i2*pi-pi/2*tanh(2*a2/(i2*pi*pi));%aproximation for k_n (Eq. 26)
for j=1:2
u=b2/(1-2*(b2*tan(b2)+a2)*sin(2*b2)/(a2*(sin(2*b2)+2*b2)))^0.5; % From article (Eq. 23), an upper bound for k_n, iterated to refine the roots
b2=u;
end
k(i2+1)=b2;
end
k=k/h;
k(:,2:N)=k(:,2:N)*complex(0,1) % print all roots where first one is real and remaining ones are purely imaginary
% Compute the matrix A for different ratios
for r = 1:length(ratios)
RI = ratios(r) * RE; % Update RI based on the current ratio
%% finding the root \lemda_m =m*pi/b%%%%%%
for j=1:M
m(j)=(pi*(j-1))/b;
end
% Initialize matrix A
A = zeros(M, N);
% Set A_00 (first element)
A(1, 1) = cosh(k(1) * h) * sinh(k(1) * b) / (k(1) * b * (2 * k(1) * h + sinh(2 * k(1)* h))) * besselh(0, 1, k(1) * RE) / (k(1) * besselh(-1, 1, k(1) * RE));
% Set A_0n for j = 2:N
for j = 2:N
A(1, j) = cos(k(j) * h) * sin(k(j) * b) / (k(j) * b * (2 * k(j) * h + sin(2 * k(j) * h))) * besselk(0, k(j) * RE) / (-k(j) * besselk(-1, k(j) * RE));
end
% Set A_m0 for i = 2:M
for i = 2:M
A(i, 1) = 2 * cosh(k(1) * h) * (-1)^(i - 1) * k(1) * sinh(k(1) * b) / (b * (2 * k(1) * h + sinh(2 * k(1) * h)) * (k(1)^2 + m(i)^2)) * besselh(0, 1, k(1) * RE) / (k(1) * besselh(-1, 1, k(1)* RE));
end
% Set A_mn for i = 2:M, j = 2:N
for i = 2:M
for j = 2:N
A(i, j) = 2 * (-1)^(i - 1) * sin(k(j) * b) / (b * (2 * k(j) * h + sin(2 * k(j) * h)) * (k(j)^2 - m(i)^2)) * besselk(0, k(j) * RE) / (-k(j) * besselk(-1, k(j) * RE));
end
end
end
% this is for one value of nu and now i want it for nu = linspace(0.01, 10, 100);% Loop over the range of nu

Più risposte (4)

I see, @javeria. So, you would like to learn how to solve a system of linear equations. You can refer to the example at the following link and begin writing the code accordingly.
If you are able to create the equations in LaTeX, I believe you can certainly write those equations in MATLAB. You can also search for more examples on Google: "how to solve a system of linear equations with MATLAB".

1 Commento

@Sam Chak for the simple equations i solved it, but here the system is very complicated because these coefficients of unknowns are also matrix not a single value. so i am little bit confuse that how i can define these equations in matlab.

Accedi per commentare.

If you want to avoid writing your system as A*X = B, you can use a nonlinear solver like "fsolve". As the system is linear, it should converge in one step.
Otherwise - as @Sam Chak already said - you can use "equationsToMatrix" to let MATLAB compute A and B from your equations:

28 Commenti

@Torsten as you can see from the screen shoot these coefficients are defined like this and the rest also.
Then you will have to compute them first before applying "fsolve" or "equationsToMatrix":
M = 20;
N = 50;
for i = 1:M
for j = 1:N
A(i,j) = ...;
end
end
for j = 1:N
for i = 1:M
B(j,i) = ...;
end
end
And you don't have 8 equations for 8 unknowns. You have 2*(M+1) + 2*(N+1) equations for the unknowns a_0,a_1,...,a_N,b_0,b_1,...,b_M,c_0,c_1,...,c_M,d_0,d_1,...,d_N.
but we have A_00 which is for m=0,n=0 then A_0n which is the case when m=0 ,n>0 , same we have A_m0 which is when n=0 ,m>0 , A_mn which is when m,n>0 .... how i can creat loops for these coefficients ?
@javeria Is a hidden variable or a constant? If it is a variable, then and are piecewise functions, making the system nonlinear.
@Sam Chak l is mode of bessel functions so we will solve the system one time for l=0 and one time for l=1.
In MATLAB, you must save the parameters in arrays with array indices increased by 1:
M = 20;
N = 50;
for i = 1:M+1
A(i,1) = ...; % this is A_(i-1),0 1 <= i <= M+1
end
for j = 2:N+1
A(1,j) = ...; % this is A_0,(j-1) 1 <= j <= N+1
end
for i = 2:M+1
for j = 2:N+1
A(i,j) = ...; % this is A_(i-1),(j-1) 2 <= i <= M+1, 2 <= j <= N+1
end
end
@javeria, I admit that at first, I did not examine your problem carefully due to the numerous subscripts and superscripts in the equations. My mind visually simplified what my eyes saw as the variables to be determined
and the rest are either coefficients in or constants in .
In fact, this series alone already has infinitely many unknowns and n approaches .
@Sam Chak @Torsten thank you so much for your positive response. Can you help me in solving my this problem. to properly design my code in matlab? the general idea you can discuss with me. Thanks
@javeria I'm unfamiliar with the terms and sequences in the equations. However, you can at least start defining all known constants.
RE = 1;
k0 = 1;
b = 1;
@Sam Chak I will upload all the related information to this problem
@javeria, I am not very familiar with sequences. Otherwise, I'd have recognized the Bessel function. However, sharing your initial code and the mathematical context allows others to understand the specific problem you are facing. You will likely receive guidance on how to proceed, but generally, you will still need to write the code step-by-step as directed.
Click on the indentation icon to start defining the constants and sequences, or directly copy/paste from your MATLAB Editor, if you already have the code.
Also, avoid sharing information incrementally, as some individuals may lose motivation in the discussion if the initially suggested solution does not address the underlying problem adequately.
Upon computing the coefficients and constant terms, the primary challenge lies in solving a very large system of n linear equations. From your image, ideally, the series should represent the sum of infinitely many terms. However, you must truncate the series by selecting an appropriate value of n based on the desired magnitude of the remainder.
I conducted some simulations based on the number of variables in the linear system, and here are the results:
  • Elapsed time is 7.818219 seconds for 100 variables.
  • Elapsed time is 77.054258 seconds for 200 variables.
  • Elapsed time is 352.742283 seconds for 300 variables.
  • Elapsed time is 1104.857874 seconds for 400 variables.
  • Elapsed time is 2743.320971 seconds for 500 variables.
tic
%% define the number of equations and variables
n = 100;
%% create symbolic variables x(1) to x(n)
syms x [1 n];
%% initialize an array to hold the equations
eqns= sym(zeros(1, n));
%% define the range for random integers
minValue = 1; % Minimum value
maxValue = 100; % Maximum value
%% generate random coefficients and constants
for i = 1:n
coeffs = randi([minValue, maxValue], 1, n); % random coefficients
constant= randi(n); % random constant term
eqns(i) = sum(coeffs.*x) == constant; % create the equation
end
%% convert to matrix form and solve the linear system
[A, b] = equationsToMatrix(eqns, x)
A = 
b = 
solx = vpa(linsolve(A, b))
solx = 
toc
Elapsed time is 9.680739 seconds.
@Sam Chak My problem involves many coefficients and values that must be predefined before solving the system. I am currently working on defining these values, which is where I am facing some difficulty. Once these coefficients and parameters are correctly defined, I will be able to proceed with solving the system step-by-step. I am looking for guidance on how to properly define these values and coefficients so that I can move forward with my solution. i need to define these coefficient matrix before solving the system. here this mode of bessel function i.e l is zero. The k_0 is find from dispersion equation for progressive mode and k_n from evenscent mode , the other parameter are defined as: \begin{itemize}
\item \( R_E = 1.0 \): The external radius of the cylinder.
\item \( h = 4 \cdot R_E \): The water depth, which is 4 times the external radius \( R_E \).
\item \( b = h - d = 2 \cdot R_E \): The distance from the bottom of the cylinder to the sea bed.
\item \( g = 9.81 \, \text{m/s}^2 \): The acceleration due to gravity.
\item \( N = 20 \): The number of terms used to truncate the series in regions E and I (external and internal regions).
\item \( M = 50 \): The number of terms used to truncate the series in region B (the region between the cylinder and the sea bed).
\item \( \text{ratios} = [1.031, 1.5, 2.0, 4.0] \): The ratios \( R_E / R_I \) that are to be tested, where \( R_I \) is the internal radius of the cylinder.
\item
\end{itemize} Additionally, we consider the dispersion relations for both progressive and evanescent modes: \begin{itemize}
\item **Dispersion equation for progressive mode**:
\begin{equation*}
\omega^2 = k g tanh(kh)
\end{equation*}
\item **Dispersion equation for evanescent mode**: This describes the behavior of the wave in which the energy decays exponentially with distance from the boundary (i.e., these waves do not propagate but decay).
\begin{equation*}
\frac{\omega^2}{g} + k_n tan(k_n h) = 0
\end{equation*}
\end{itemize}
I am looking for guidance on how to properly define these values and coefficients so that I can move forward with my solution.
You expect guidance in determining physical constants ? In a MATLAB forum ?
clc;
close all;
clear all;
N = 20;
M = 50;
RI = 1.0; % Radius of inner cylinder
ratios = [1.031,1.5, 2.0, 4.0]; % Ratios RE/RI you want to test
b = 2.0*RI; % The distance from seabed to the bottom of cylinder (h-d)/a = 2
h = 4.0*RI; % Water depth d/a = 4.
g = 9.81; % Gravity acceleration
X_sqrt_kRI = linspace(0.1, 1.5, 2000); % Given the value of sqrt(kRI)
I_given_wavenumber = 1;
for r = 1:length(ratios)
RE = ratios(r) * RI; % Update RI based on the current ratio
for j = 1:length(X_sqrt_kRI)
if I_given_wavenumber == 1
wk = X_sqrt_kRI(j)^2/RI; %wavenumber
omega = sqrt(g*wk*tanh(wk*h)); %wave radian frequency
fun_alpha = @(x) omega^2 + g*x*tan(x*h); %dispersion equation for evanescent modes
end
for n = 1:N
if n ==1
k(n) = -1i*wk;
else
x0_n = [(2*n -3)*pi/2./b, (2*n -1)*pi/2./b];
k(n) = fzero(fun_alpha, mean(x0_n));
end
end
end
end
I solved the dispersion equation for progressive mode and for evenscent mode like this now after that i want to define the coefficients A_{00}^{l} , A_{0n}^{l}, A_{m0}^{l} and A_{mn}^{l}. so i need guidence that how i can set these coefficients. Also where we have k_0 then this value is wk, and when we have k_n then this is value from evenscent equation.
H_l(k_0*R_I) is besselh(l,k(1)*RI), K_l(k_n*RI) is besselk(l,k(n+1)*RI) ?
And here are the recursion formulas for the derivatives:
% Define matrix size
M = 3; % number of rows (m values)
N = 3; % number of columns (n values)
% Initialize the matrix
A = zeros(M, N); % Matrix to store the coefficients
% Loop through the matrix
for i = 1:M
for j = 1:N
i write this code for these coefficients am i doing correctly? for A_{00}^{l} , A_{0n}^{l}, A_{m0}^{l} and A_{mn}^{l}??
% Determine the value of A_l^mn based on conditions for m and n
if m == 1 && n == 1
% A_l^{00}, the first element in the matrix (m=1, n=1 corresponds to A_l^{00})
A(i, j) = A_l_00(m, n); % Replace with the formula for A_l^{00}
elseif m == 1 && n > 1
% A_l^{0n}, when m = 1 and n > 1
A(i, j) = A_l_0n(m, n); % Replace with the formula for A_l^{0n}
elseif m > 1 && n == 1
% A_l^{m0}, when m > 1 and n = 1
A(i, j) = A_l_m0(m, n); % Replace with the formula for A_l^{m0}
elseif m > 1 && n > 1
% A_l^{mn}, when m > 1 and n > 1
A(i, j) = A_l_mn(m, n); % Replace with the formula for A_l^{mn}
end
end
end
% Display the matrix
disp(A);
Why not doing it as I suggested: in three separate loops ?
A = zeros(M,N);
A(1,1) = ...; % Set A_00 according to formula 1
for j = 2:N
A(1,j) ...; % Set A_0n according to formula 2
end
for i = 2:M
A(i,1) = ...; % Set A_m0 according to formula 3
end
for i = 2:M
for j = 2:N
A(i,j) = ...; % Set A_mn according to formula 4
end
end
@Torsten @Sam Chak Okay thanks. i will define all these coeffiients before solving the system. So when i came to the linear equations solving part then I am little bit confuse that how i can now formulate my system of equations in matrix form that it take the previosly defined coefficients matrix correctly.
No @javeria, you do not need to define all these coefficients at once in your next post. Instead, make your post and code progressive. Focus on getting at least one part of the code working first for easy troubleshooting.
For example, define the coefficients for the series . Use small values for m and n to verify whether the suggested for-loop approach functions appropriately. If successful, then implement the code for the , , and series.
After you defined the matrix A and the vector Z,
Z = ...;
a = sym('a',[N 1]);
b = sym('b',[M 1]);
eqns12 = b == A*a + Z;
would define the first two lines of equations you included with a given M x 1 vector Z and unknown vectors a of size N x 1 and b of size M x 1 .
I write a matlab code to solve the system , but i am facing some confusions regarding it, this one works but when i extract the b, a , c , d then it give me wrong vectors. Also when i define the b a c d x later in the end then it give me error.
% Define dimensions
M = 3; % For m = 1, 2, 3
N = 2; % For n = 1, 2
b = sym('b', [1 M]); % b1, b2, b3
a = sym('a', [1 N]); % a1, a2
c = sym('c', [1 M]); % c1, c2, c3
d = sym('d', [1 N]); % d1, d2
x = [b, a, c, d]'; % Vector of unknowns
% Initialize coefficient matrices
A = zeros(M, N); % 4x3 (Aij)
B = zeros(N, M); % 3x4 (Bij)
C = zeros(N, M); % 3x4 (Cij)
D = zeros(M, N); % 4x3 (Dij)
E = zeros(N, M); % 3x4 (Eij)
F = zeros(N, M); % 3x4 (Fij)
% Define A (Aij)
A(1,1) = 2;
for j = 2:N
A(1,j) = 3;
end
for i = 2:M
A(i,1) = 4;
end
for i = 2:M
for j = 2:N
A(i,j) = 5;
end
end
% Define B (Bij)
B(1,1) = 1;
for j = 2:M
B(1,j) = 2;
end
for i = 2:N
B(i,1) = 3;
end
for i = 2:N
for j = 2:M
B(i,j) = 1;
end
end
% Define C (Cij)
C(1,1) = 2;
for j = 2:M
C(1,j) = 1;
end
for i = 2:N
C(i,1) = 0;
end
for i = 2:N
for j = 2:M
C(i,j) = 2;
end
end
% Define D (Dij)
D(1,1) = 1;
for j = 2:N
D(1,j) = 2;
end
for i = 2:M
D(i,1) = 3;
end
for i = 2:M
for j = 2:N
D(i,j) = 1;
end
end
% Define E (Eij)
E(1,1) = 1;
for j = 2:M
E(1,j) = 2;
end
for i = 2:N
E(i,1) = 3;
end
for i = 2:N
for j = 2:M
E(i,j) = 1;
end
end
% Define F (Fij)
F(1,1) = 2;
for j = 2:M
F(1,j) = 1;
end
for i = 2:N
F(i,1) = 0;
end
for i = 2:N
for j = 2:M
F(i,j) = 2;
end
end
% Define right-hand side vectors
Z = zeros(M, 1); % Z0, Z1, Z2, Z3
Y = zeros(N, 1); % Y0, Y1, Y2
X = zeros(M, 1); % X0, X1, X2, X3
W = zeros(N, 1); % W0, W1, W2
% Define Z (start at 1 and increment by 1)
Z(1) = 1;
for i = 2:M
Z(i) = 2;
end
% Define Y (start at 5 and increment by 1)
Y(1) = 5;
for i = 2:N
Y(i) = 9;
end
% Define X (start at 8 and increment by 1)
X(1) = 8;
for i = 2:M
X(i) = 9;
end
% Define W (start at 12 and increment by 1)
W(1) = 12;
for i = 2:N
W(i) = 10;
end
% Define identity and zero submatrices
I1 = zeros(M, M); % 4x4 identity
I2 = zeros(N, N); % 3x3 identity
I3 = zeros(M, M); % 4x4 identity
I4 = zeros(N, N); % 3x3 identity
for i = 1:M
I1(i,i) = 1;
I3(i,i) = 1;
end
for i = 1:N
I2(i,i) = 1;
I4(i,i) = 1;
end
O1 = zeros(M, M); % 4x4 zero
O2 = zeros(M, N); % 4x3 zero
O3 = zeros(N, M); % 3x4 zero
O4 = zeros(N, N); % 3x3 zero
O5 = zeros(M, M); % 4x4 zero
O6 = zeros(M, N); % 4x3 zero
O7 = zeros(N, N); % 3x3 zero
% Assemble the full matrix M
M = [I1, -A, O1, O2;
-B, I2, -C, O4;
O5, O6, I3, -D;
-E, O7, -F, I4];
% Assemble the vector b
y = [Z; Y; X; W];
% Solve the system
x_solution = inv(M) * y;
% Display all components
% Display results
disp('Matrix M (14x14):'); disp(M);
disp('Vector b (14x1):'); disp(b);
disp('Solution vector x:'); disp(x_solution);
% Extract bm, an, cm, dn from x_solution
bm = x_solution(1:M); % b0, b1, b2, b3
an = x_solution(M+1:M+N); % a0, a1, a2
cm = x_solution(M+N+1:M+N+M); % c0, c1, c2, c3
dn = x_solution(M+N+M+1:end); % d0, d1, d2
disp('bm (b0, b1, b2, b3):'); disp(bm);
disp('an (a0, a1, a2):'); disp(an);
disp('cm (c0, c1, c2, c3):'); disp(cm);
disp('dn (d0, d1, d2):'); disp(dn);
% Define dimensions
M = 2; % For m = 0, 1, 2, 3
N = 1; % For n = 0, 1, 2
% Initialize coefficient matrices
A = zeros(M, N); % 4x3 (Aij)
B = zeros(N, M); % 3x4 (Bij)
C = zeros(N, M); % 3x4 (Cij)
D = zeros(M, N); % 4x3 (Dij)
E = zeros(N, M); % 3x4 (Eij)
F = zeros(N, M); % 3x4 (Fij)
% Define A (Aij)
A(1,1) = 2;
for j = 2:N
A(1,j) = 3;
end
for i = 2:M
A(i,1) = 4;
end
for i = 2:M
for j = 2:N
A(i,j) = 5;
end
end
% Define B (Bij)
B(1,1) = 1;
for j = 2:M
B(1,j) = 2;
end
for i = 2:N
B(i,1) = 3;
end
for i = 2:N
for j = 2:M
B(i,j) = 1;
end
end
% Define C (Cij)
C(1,1) = 2;
for j = 2:M
C(1,j) = 1;
end
for i = 2:N
C(i,1) = 0;
end
for i = 2:N
for j = 2:M
C(i,j) = 2;
end
end
% Define D (Dij)
D(1,1) = 1;
for j = 2:N
D(1,j) = 2;
end
for i = 2:M
D(i,1) = 3;
end
for i = 2:M
for j = 2:N
D(i,j) = 1;
end
end
% Define E (Eij)
E(1,1) = 1;
for j = 2:M
E(1,j) = 2;
end
for i = 2:N
E(i,1) = 3;
end
for i = 2:N
for j = 2:M
E(i,j) = 1;
end
end
% Define F (Fij)
F(1,1) = 2;
for j = 2:M
F(1,j) = 1;
end
for i = 2:N
F(i,1) = 0;
end
for i = 2:N
for j = 2:M
F(i,j) = 2;
end
end
% Define right-hand side vectors
Z = zeros(M, 1); % Z0, Z1, Z2, Z3
Y = zeros(N, 1); % Y0, Y1, Y2
X = zeros(M, 1); % X0, X1, X2, X3
W = zeros(N, 1); % W0, W1, W2
% Define Z (start at 1 and increment by 1)
Z(1) = 1;
for i = 2:M
Z(i) = 2;
end
% Define Y (start at 5 and increment by 1)
Y(1) = 5;
for i = 2:N
Y(i) = 9;
end
% Define X (start at 8 and increment by 1)
X(1) = 8;
for i = 2:M
X(i) = 9;
end
% Define W (start at 12 and increment by 1)
W(1) = 12;
for i = 2:N
W(i) = 10;
end
% Define identity and zero submatrices
I1 = zeros(M, M); % 4x4 identity
I2 = zeros(N, N); % 3x3 identity
I3 = zeros(M, M); % 4x4 identity
I4 = zeros(N, N); % 3x3 identity
for i = 1:M
I1(i,i) = 1;
I3(i,i) = 1;
end
for i = 1:N
I2(i,i) = 1;
I4(i,i) = 1;
end
O1 = zeros(M, M); % 4x4 zero
O2 = zeros(M, N); % 4x3 zero
O3 = zeros(N, M); % 3x4 zero
O4 = zeros(N, N); % 3x3 zero
O5 = zeros(M, M); % 4x4 zero
O6 = zeros(M, N); % 4x3 zero
O7 = zeros(N, N); % 3x3 zero
% Assemble the full matrix M
M = [I1, -A, O1, O2;
-B, I2, -C, O4;
O5, O6, I3, -D;
-E, O7, -F, I4];
% Assemble the vector y
y = [Z; Y; X; W];
b = sym('b', [1 M]); % b0, b1, b2, b3
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
a = sym('a', [1 N]); % a0, a1, a2
c = sym('c', [1 M]); % c0, c1, c2, c3
d = sym('d', [1 N]); % d0, d1, d2
x = [b, a, c, d]'; % Vector of unknowns
% Solve the system
x_solution = inv(M) * y;
% Display all components
% Display results
disp('Matrix M (14x14):'); disp(M);
disp('Vector b (14x1):'); disp(b);
disp('Solution vector x:'); disp(x_solution);
% Extract bm, an, cm, dn from x_solution
bm = x_solution(1:M); % b0, b1, b2, b3
an = x_solution(M+1:M+N); % a0, a1, a2
cm = x_solution(M+N+1:M+N+M); % c0, c1, c2, c3
dn = x_solution(M+N+M+1:end); % d0, d1, d2
disp('bm (b0, b1, b2, b3):'); disp(bm);
disp('an (a0, a1, a2):'); disp(an);
disp('cm (c0, c1, c2, c3):'); disp(cm);
disp('dn (d0, d1, d2):'); disp(dn);
this one code give me error
% Define dimensions
M = 3; % For m = 1, 2, 3
N = 2; % For n = 1, 2
a = sym('a', [N 1]); % a1, a2
b = sym('b', [M 1]); % b1, b2, b3
c = sym('c', [M 1]); % c1, c2, c3
d = sym('d', [N 1]); % d1, d2
% Initialize coefficient matrices
A = zeros(M, N); % 4x3 (Aij)
B = zeros(N, M); % 3x4 (Bij)
C = zeros(N, M); % 3x4 (Cij)
D = zeros(M, N); % 4x3 (Dij)
E = zeros(N, M); % 3x4 (Eij)
F = zeros(N, M); % 3x4 (Fij)
% Define A (Aij)
A(1,1) = 2;
for j = 2:N
A(1,j) = 3;
end
for i = 2:M
A(i,1) = 4;
end
for i = 2:M
for j = 2:N
A(i,j) = 5;
end
end
% Define B (Bij)
B(1,1) = 1;
for j = 2:M
B(1,j) = 2;
end
for i = 2:N
B(i,1) = 3;
end
for i = 2:N
for j = 2:M
B(i,j) = 1;
end
end
% Define C (Cij)
C(1,1) = 2;
for j = 2:M
C(1,j) = 1;
end
for i = 2:N
C(i,1) = 0;
end
for i = 2:N
for j = 2:M
C(i,j) = 2;
end
end
% Define D (Dij)
D(1,1) = 1;
for j = 2:N
D(1,j) = 2;
end
for i = 2:M
D(i,1) = 3;
end
for i = 2:M
for j = 2:N
D(i,j) = 1;
end
end
% Define E (Eij)
E(1,1) = 1;
for j = 2:M
E(1,j) = 2;
end
for i = 2:N
E(i,1) = 3;
end
for i = 2:N
for j = 2:M
E(i,j) = 1;
end
end
% Define F (Fij)
F(1,1) = 2;
for j = 2:M
F(1,j) = 1;
end
for i = 2:N
F(i,1) = 0;
end
for i = 2:N
for j = 2:M
F(i,j) = 2;
end
end
% Define right-hand side vectors
Z = zeros(M, 1); % Z0, Z1, Z2, Z3
Y = zeros(N, 1); % Y0, Y1, Y2
X = zeros(M, 1); % X0, X1, X2, X3
W = zeros(N, 1); % W0, W1, W2
% Define Z (start at 1 and increment by 1)
Z(1) = 1;
for i = 2:M
Z(i) = 2;
end
% Define Y (start at 5 and increment by 1)
Y(1) = 5;
for i = 2:N
Y(i) = 9;
end
% Define X (start at 8 and increment by 1)
X(1) = 8;
for i = 2:M
X(i) = 9;
end
% Define W (start at 12 and increment by 1)
W(1) = 12;
for i = 2:N
W(i) = 10;
end
eqn1 = A*a-b+Z==0;
eqn2 = B*b+C*c-a+Y==0;
eqn3 = D*d-c+X==0;
eqn4 = E*b+F*c+W-d==0;
eqns = [eqn1;eqn2;eqn3;eqn4];
vars = [a;b;c;d];
[AA,bb] = equationsToMatrix(eqns,vars);
sol = AA\bb;
asol = sol(1:N)
asol = 
bsol = sol(N+1:N+M)
bsol = 
csol = sol(N+M+1:N+M+M)
csol = 
dsol = sol(N+M+M+1:N+M+M+N)
dsol = 
When N and M get larger, you should use
sol = double(AA)\double(bb);
asol = sol(1:N)
asol = 2×1
-8.0392 3.1373
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
bsol = sol(N+1:N+M)
bsol = 3×1
-5.6667 -14.4706 -14.4706
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
csol = sol(N+M+1:N+M+M)
csol = 3×1
15.2353 10.0196 10.0196
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
dsol = sol(N+M+M+1:N+M+M+N)
dsol = 2×1
-1.0392 4.1373
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
when i convert my system of equations into matrix form then we have unite digonal submatrices also.
when i run your code in my system matlab it give me this error Error using sym/horzcat
Execution of script horzcat as a function is not supported:
C:\Program Files\MATLAB\R2023b\toolbox\symbolic\symbolic\@sym\horzcat.m
Error in sym.getEqnsVars (line 61)
eqns = [eqns{:}];
Error in sym/equationsToMatrix (line 55)
[eqns,vars] = sym.getEqnsVars(argv{:});
Error in untitled2 (line 124)
[AA,bb] = equationsToMatrix(eqns,vars);
but online i checked it doesnot give any error
Did you copy my full code ?
I changed
b = sym('b', [1 M]); % b1, b2, b3
a = sym('a', [1 N]); % a1, a2
c = sym('c', [1 M]); % c1, c2, c3
d = sym('d', [1 N]); % d1, d2
to
a = sym('a', [N 1]); % a1, a2
b = sym('b', [M 1]); % b1, b2, b3
c = sym('c', [M 1]); % c1, c2, c3
d = sym('d', [N 1]); % d1, d2
Maybe this is the problem.

Accedi per commentare.

% Define dimensions
M = 4; % For m = 0, 1, 2, 3
N = 3; % For n = 0, 1, 2
% Initialize coefficient matrices
A = zeros(M, N); % 4x3 (Aij)
B = zeros(N, M); % 3x4 (Bij)
C = zeros(N, M); % 3x4 (Cij)
D = zeros(M, N); % 4x3 (Dij)
E = zeros(N, M); % 3x4 (Eij)
F = zeros(N, M); % 3x4 (Fij)
% Define A (Aij)
A(1,1) = 2;
for j = 2:N
A(1,j) = 3;
end
for i = 2:M
A(i,1) = 4;
end
for i = 2:M
for j = 2:N
A(i,j) = 5;
end
end
% Define B (Bij)
B(1,1) = 1;
for j = 2:M
B(1,j) = 2;
end
for i = 2:N
B(i,1) = 3;
end
for i = 2:N
for j = 2:M
B(i,j) = 1;
end
end
% Define C (Cij)
C(1,1) = 2;
for j = 2:M
C(1,j) = 1;
end
for i = 2:N
C(i,1) = 0;
end
for i = 2:N
for j = 2:M
C(i,j) = 2;
end
end
% Define D (Dij)
D(1,1) = 1;
for j = 2:N
D(1,j) = 2;
end
for i = 2:M
D(i,1) = 3;
end
for i = 2:M
for j = 2:N
D(i,j) = 1;
end
end
% Define E (Eij)
E(1,1) = 1;
for j = 2:M
E(1,j) = 2;
end
for i = 2:N
E(i,1) = 3;
end
for i = 2:N
for j = 2:M
E(i,j) = 1;
end
end
% Define F (Fij)
F(1,1) = 2;
for j = 2:M
F(1,j) = 1;
end
for i = 2:N
F(i,1) = 0;
end
for i = 2:N
for j = 2:M
F(i,j) = 2;
end
end
% Define right-hand side vectors
Z = zeros(M, 1); % Z0, Z1, Z2, Z3
Y = zeros(N, 1); % Y0, Y1, Y2
X = zeros(M, 1); % X0, X1, X2, X3
W = zeros(N, 1); % W0, W1, W2
% Define Z (start at 1 and increment by 1)
Z(1) = 1;
for i = 2:M
Z(i) = 2;
end
% Define Y (start at 5 and increment by 1)
Y(1) = 5;
for i = 2:N
Y(i) = 9;
end
% Define X (start at 8 and increment by 1)
X(1) = 8;
for i = 2:M
X(i) = 9;
end
% Define W (start at 12 and increment by 1)
W(1) = 12;
for i = 2:N
W(i) = 10;
end
% Define identity and zero submatrices
I1 = zeros(M, M); % 4x4 identity
I2 = zeros(N, N); % 3x3 identity
I3 = zeros(M, M); % 4x4 identity
I4 = zeros(N, N); % 3x3 identity
for i = 1:M
I1(i,i) = 1;
I3(i,i) = 1;
end
for i = 1:N
I2(i,i) = 1;
I4(i,i) = 1;
end
O1 = zeros(M, M); % 4x4 zero
O2 = zeros(M, N); % 4x3 zero
O3 = zeros(N, M); % 3x4 zero
O4 = zeros(N, N); % 3x3 zero
O5 = zeros(M, M); % 4x4 zero
O6 = zeros(M, N); % 4x3 zero
O7 = zeros(N, N); % 3x3 zero
% Assemble the full matrix G
G = [I1, -A, O1, O2;
-B, I2, -C, O4;
O5, O6, I3, -D;
-E, O7, -F, I4];
% Assemble the vector b
y = [Z; Y; X; W];
b = sym('b', [1 ,M]); % b0, b1, b2, b3
a = sym('a', [1, N]); % a0, a1, a2
c = sym('c', [1 ,M]); % c0, c1, c2, c3
d = sym('d', [1, N]); % d0, d1, d2
x = [b, a, c, d]'; % Vector of unknowns
% Solve the system
x_solution = inv(G) * y;
% Display all components
% Display results
disp('Matrix M (14x14):'); disp(M);
Matrix M (14x14): 4
disp('Vector b (14x1):'); disp(b);
Vector b (14x1):
disp('Solution vector x:'); disp(x_solution);
Solution vector x: -3.1481 -11.8201 -11.8201 -11.8201 -10.3598 2.7619 2.7619 19.6878 6.4444 6.4444 6.4444 -3.3598 3.7619 3.7619
% Extract bm, an, cm, dn from x_solution
bm = x_solution(1:M); % b0, b1, b2, b3
an = x_solution(M+1:M+N); % a0, a1, a2
cm = x_solution(M+N+1:M+N+M); % c0, c1, c2, c3
dn = x_solution(M+N+M+1:end); % d0, d1, d2
disp('bm (b0, b1, b2, b3):'); disp(bm);
bm (b0, b1, b2, b3): -3.1481 -11.8201 -11.8201 -11.8201
disp('an (a0, a1, a2):'); disp(an);
an (a0, a1, a2): -10.3598 2.7619 2.7619
disp('cm (c0, c1, c2, c3):'); disp(cm);
cm (c0, c1, c2, c3): 19.6878 6.4444 6.4444 6.4444
disp('dn (d0, d1, d2):'); disp(dn);
dn (d0, d1, d2): -3.3598 3.7619 3.7619
when i run my this code online it works but on my matlab it give me error i donot why

20 Commenti

% Define dimensions
M = 3; % For m = 0, 1, 2, 3
N = 2; % For n = 0, 1, 2
% Initialize coefficient matrices
A = zeros(M, N); % 4x3 (Aij)
B = zeros(N, M); % 3x4 (Bij)
C = zeros(N, M); % 3x4 (Cij)
D = zeros(M, N); % 4x3 (Dij)
E = zeros(N, M); % 3x4 (Eij)
F = zeros(N, M); % 3x4 (Fij)
% Define A (Aij)
A(1,1) = 2;
for j = 2:N
A(1,j) = 3;
end
for i = 2:M
A(i,1) = 4;
end
for i = 2:M
for j = 2:N
A(i,j) = 5;
end
end
% Define B (Bij)
B(1,1) = 1;
for j = 2:M
B(1,j) = 2;
end
for i = 2:N
B(i,1) = 3;
end
for i = 2:N
for j = 2:M
B(i,j) = 1;
end
end
% Define C (Cij)
C(1,1) = 2;
for j = 2:M
C(1,j) = 1;
end
for i = 2:N
C(i,1) = 0;
end
for i = 2:N
for j = 2:M
C(i,j) = 2;
end
end
% Define D (Dij)
D(1,1) = 1;
for j = 2:N
D(1,j) = 2;
end
for i = 2:M
D(i,1) = 3;
end
for i = 2:M
for j = 2:N
D(i,j) = 1;
end
end
% Define E (Eij)
E(1,1) = 1;
for j = 2:M
E(1,j) = 2;
end
for i = 2:N
E(i,1) = 3;
end
for i = 2:N
for j = 2:M
E(i,j) = 1;
end
end
% Define F (Fij)
F(1,1) = 2;
for j = 2:M
F(1,j) = 1;
end
for i = 2:N
F(i,1) = 0;
end
for i = 2:N
for j = 2:M
F(i,j) = 2;
end
end
% Define right-hand side vectors
Z = zeros(M, 1); % Z0, Z1, Z2, Z3
Y = zeros(N, 1); % Y0, Y1, Y2
X = zeros(M, 1); % X0, X1, X2, X3
W = zeros(N, 1); % W0, W1, W2
% Define Z (start at 1 and increment by 1)
Z(1) = 1;
for i = 2:M
Z(i) = 2;
end
% Define Y (start at 5 and increment by 1)
Y(1) = 5;
for i = 2:N
Y(i) = 9;
end
% Define X (start at 8 and increment by 1)
X(1) = 8;
for i = 2:M
X(i) = 9;
end
% Define W (start at 12 and increment by 1)
W(1) = 12;
for i = 2:N
W(i) = 10;
end
G = [A,-eye(M),zeros(M),zeros(M,N);...
-eye(N),B,C,zeros(N);...
zeros(M,N),zeros(M),-eye(M),D;...
zeros(N),E,F,-eye(N)];
rhs = [-Z;-Y;-X;-W];
% Solve the system
x_solution = G\rhs;
asol = x_solution(1:N)
asol = 2×1
-8.0392 3.1373
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
bsol = x_solution(N+1:N+M)
bsol = 3×1
-5.6667 -14.4706 -14.4706
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
csol = x_solution(N+M+1:N+M+M)
csol = 3×1
15.2353 10.0196 10.0196
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
dsol = x_solution(N+M+M+1:N+M+M+N)
dsol = 2×1
-1.0392 4.1373
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Seems you don't have a licence to use the Symbolic Toolbox. I'm surprised you didn't notice that earlier.
Thus you should use the pure numerical code without symbolic variables from above (or buy a licence).
@Torsten yes i buy licence now but i was also surprised that my matlab was working then another day it doesnot working
The Symbolic Math Toolbox is included in the basic version of MATLAB Online, which can be used freely for 20 hours per month. You can test @Torsten's code there, and if both M and N are relatively small integers, it should work as long as it does not continuously compute for over 15 minutes. Additionally, MATLAB Online will automatically sign out after 15 minutes of idle time. These are the only drawbacks.
Even if you are away from your computer, you can also conveniently connect to MATLAB Online from your iPhone, iPad, or Android phone when you install the MATLAB Mobile app.
@javeria So you already ran a code successfully on your PC that used the Symbolic Toolbox ?
@Sam Chak @Torsten i have successfully solve the dispersion relation for wave number. Now i want jus to calculate the Matrix A and then i want to plot my results against k_0RE. where this k_0 is the 1st column of k values. I am facing confusion that how i can set my this code properly.
% get_wave_numbers: Compute k0, k1, ..., kN using Chamberlin & Porter
nu = linspace(0.01, 10, 100); % 100 points from 0.01 to 10 for omega (nu)
h =4.0;
N = 20 ; %(k0 + N-1 evanescent)
% Returns vector of roots k = [k0, k1, ..., kN] (dimensional)
k = zeros(length(nu), N); % Efficient preallocation of the k matrix % Preallocate
% % Use the algorithm of Chamberlin & Porter
for idx = 1:length(nu) % Loop over the range of nu
a2=nu(idx)*h;
p=a2*(1-4*(1-(1+a2)*exp(-2*a2))/(2*a2+sinh(2*a2)))^(-0.25); % approximation for k_0 (Eq. 19)
k(idx, 1) = p; % Store k_0
% Evanescent roots k1, ..., kN-1 using Eq. 26 + Eq. 23
for i2=1:N-1
b=i2*pi-pi/2*tanh(2*a2/(i2*pi*pi));%aproximation for k_n (Eq. 26)
for j=1:2
u=b/(1-2*(b*tan(b)+a2)*sin(2*b)/(a2*(sin(2*b)+2*b)))^.5; % From article (Eq. 23), an upper bound for k_n, iterated to refine the roots
b=u;
end
k(idx, i2 + 1) = b; % Store the result for k_n
end
% Convert all to dimensional values by dividing by h
k(idx, :) = k(idx, :) / h;
% Make k1...kN imaginary (evanescent)
k(idx, 2:N) = k(idx, 2:N) * complex(0, 1);
end
end
next i want to make use of these values for matrix A and then plot result against k_0RE .
clc;
close all;
clear all;
tic;
N = 20; % Number of terms in region E and I
M= 50; % Number of term in region B
RE = 1.0; % Radius of external cylinder
ratios = [1.031,1.5, 2.0, 4.0]; % Ratios RE/RI you want to test
b = 2.0*RE; % The distance from seabed to the bottom of cylinder (h-d)=b
h = 4.0*RE; % Water depth.
g = 9.81; % Gravity acceleration
for r = 1:length(ratios)
RI = ratios(r) * RE; % Update RI based on the current ratio
%%%%%%%%%%%%%%%%%%%%%%%%%
%% finding the root \lemda_m =m*pi/b%%%%%%
for j=1:M
m(j)=(pi*(j-1))/b;
end
%%%%%%%%%%%%%%%%%%%%% Define matrix A %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
A = zeros(M,N);
A(1,1) = cosh(k0*h)*sinh(k0*b)/(k0*b*(2*k0*h+sinh(2*k0*h)))*besselh(0., 1., k0*RE) / (k0 * (besselh(-1., 1., k0*RE))); % Set A_00
for j = 2:N
A(1,j) =cos(k(j)*h)*sin(k(j)*b) / (k(j)*b*(2*k(j)*h + sin(2*k(j)*h)))* besselk(0, k(j)*RE) / (-k(j)*besselk(-1, k(j)*RE)); % Set A_0n
end
for i = 2:M
A(i,1) =2 * cosh(k0*h) * (-1)^(i-1) * k0*sinh(k0*b) / (b * (2*k0*h + sinh(2*k0*h)) * (k0^2 + (m(i)^2))) * besselh(0, 1, k0*RE) / (k0*(besselh(-1, 1, k0*RE))); % Set A_m0
end
for i = 2:M
for j = 2:N
A(i,j) = 2 * (-1)^(i-1) * sin(k(j)*b) / (b * (2*k(j)*h + sin(2*k(j)*h)) * (k(j)^2 - m(i)^2)) * besselk(0, k(j)*RE) / (-k(j)*besselk(-1, k(j)*RE));% Set A_mn
end
end
end
like this just for refrence that i want my x axis like this
I don't understand how you want to plot an (MxN) matrix against a 100x1 vector.
And does this question have anything to do with your original one ?
hello yes this is one of my question coding part. As i write now my system of linear equations in matrix form and now i want to plot one of result. I solved dispersion equation for k_0,k1,k2,....,kN where this k_0 is real and it represent progressive mode and other k2,k3,...,kN are evenscent mode i.e decaying . my first question is that how i can call the function get wave number in my main file? and the second one that i need plot of results against k_0RE . So for each nu(omega) we have a column vector of k_0 i.e real part now how i can creat a plot of k_0RE? I know we cann't plot for M*N matrix but as depict in image on y-axis we have another value and i want to solve for it.
Thanks
I write code for one value of nu(omega) now i want it for range of nu values. here is the code for single value of nu(omega)
clc;
close all;
clear all;
tic;
N = 20; % Number of terms in region E and I
M= 50; % Number of term in region B
RE = 1.0; % Radius of external cylinder
ratios = [1.031,1.5, 2.0, 4.0]; % Ratios RE/RI you want to test
b= 2.0*RE; % The distance from seabed to the bottom of cylinder (h-d)=b
h = 4.0*RE; % Water depth.
g = 9.81; % Gravity acceleration
% Preallocate wave numbers k
nu =0.01; % nu
k=linspace(0,0,N);
% % Use the algorithm of Chamberlin & Porter
a2=nu*h;
p=a2*(1-4*(1-(1+a2)*exp(-2*a2))/(2*a2+sinh(2*a2)))^(-0.25); % approximation for k_0 (Eq. 19)
k(1)=p;
for i2=1:N-1
b2=i2*pi-pi/2*tanh(2*a2/(i2*pi*pi));%aproximation for k_n (Eq. 26)
for j=1:2
u=b2/(1-2*(b2*tan(b2)+a2)*sin(2*b2)/(a2*(sin(2*b2)+2*b2)))^0.5; % From article (Eq. 23), an upper bound for k_n, iterated to refine the roots
b2=u;
end
k(i2+1)=b2;
end
k=k/h;
k(:,2:N)=k(:,2:N)*complex(0,1) % print all roots where first one is real and remaining ones are purely imaginary
% Compute the matrix A for different ratios
for r = 1:length(ratios)
RI = ratios(r) * RE; % Update RI based on the current ratio
%% finding the root \lemda_m =m*pi/b%%%%%%
for j=1:M
m(j)=(pi*(j-1))/b;
end
% Initialize matrix A
A = zeros(M, N);
% Set A_00 (first element)
A(1, 1) = cosh(k(1) * h) * sinh(k(1) * b) / (k(1) * b * (2 * k(1) * h + sinh(2 * k(1)* h))) * besselh(0, 1, k(1) * RE) / (k(1) * besselh(-1, 1, k(1) * RE));
% Set A_0n for j = 2:N
for j = 2:N
A(1, j) = cos(k(j) * h) * sin(k(j) * b) / (k(j) * b * (2 * k(j) * h + sin(2 * k(j) * h))) * besselk(0, k(j) * RE) / (-k(j) * besselk(-1, k(j) * RE));
end
% Set A_m0 for i = 2:M
for i = 2:M
A(i, 1) = 2 * cosh(k(1) * h) * (-1)^(i - 1) * k(1) * sinh(k(1) * b) / (b * (2 * k(1) * h + sinh(2 * k(1) * h)) * (k(1)^2 + m(i)^2)) * besselh(0, 1, k(1) * RE) / (k(1) * besselh(-1, 1, k(1)* RE));
end
% Set A_mn for i = 2:M, j = 2:N
for i = 2:M
for j = 2:N
A(i, j) = 2 * (-1)^(i - 1) * sin(k(j) * b) / (b * (2 * k(j) * h + sin(2 * k(j) * h)) * (k(j)^2 - m(i)^2)) * besselk(0, k(j) * RE) / (-k(j) * besselk(-1, k(j) * RE));
end
end
end
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% this is for one value of nu and now i want it for nu = linspace(0.01, 10, 100);% Loop over the range of nu
@Torsten @Sam Chak i write this code but at the end the coefficient A_0 give me NaN for some value.. however the pre calculated values are okay. I did not know the source of bug that where i did mistake . in defining this coefficient please help me in finding this source of bug .
clc;
close all;
clear all;
tic;
N = 20; % Number of terms in region E and I
M= 50; % Number of term in region B
RE = 1.0; % Radius of external cylinder
ratios = [1.031,1.5, 2.0, 4.0]; % Ratios RE/RI you want to test
b= 2.0*RE; % The distance from seabed to the bottom of cylinder (h-d)=b
h = 4.0*RE; % Water depth.
nu = linspace(0.01, 10, 100);% Loop over the range of nu
%% finding the root \lemda_m =m*pi/b%%%%%%
for j=1:M
m(j)=(pi*(j-1))/b;
end
for r = 1:length(ratios)
RI = RE / ratios(r); % Update RI based on the current ratio
% coef_A_0_all = []; % To store results for different ratios
% Preallocate wave numbers k
k = zeros(length(nu), N); % Preallocate k matrix
% Compute wave numbers k using the algorithm of Chamberlin & Porter
for idx = 1:length(nu) % Loop over the range of nu
a2=nu(idx)*h;
p=a2*(1-4*(1-(1+a2)*exp(-2*a2))/(2*a2+sinh(2*a2)))^(-0.25); % approximation for k_0 (Eq. 19)
k(idx,1) = p; % Store k_0
% Compute evanescent roots k1, ..., kN-1 using Eq. 26 + Eq. 23
for i2=1:N-1
b2=i2*pi-pi/2*tanh(2*a2/(i2*pi*pi));
for j=1:2
u=b2/(1-2*(b2*tan(b2)+a2)*sin(2*b2)/(a2*(sin(2*b2)+2*b2)))^0.5;
b2=u;
end
k(idx, i2 + 1) = b2; % Store the result for k_n
end
% Convert all to dimensional values by dividing by h
k(idx, :) = k(idx, :) / h;
% Make k1...kN imaginary (evanescent)
k(idx, 2:N) = k(idx, 2:N) * complex(0, 1);
% Compute the matrix A for different ratios
% Initialize matrix A
A = zeros(M, N);
% Set A_00 (first element)
A(1, 1) = cosh(k(1) * h) * sinh(k(1) * b) / (k(1) * b * (2 * k(1) * h + sinh(2 * k(1)* h))) * besselh(0, 1, k(1) * RE) / (k(1) * besselh(-1, 1, k(1) * RE));
% Set A_0n for j = 2:N
for j = 2:N
A(1, j) = cos(k(j) * h) * sin(k(j) * b) / (k(j) * b * (2 * k(j) * h + sin(2 * k(j) * h))) * besselk(0, k(j) * RE) / (-k(j) * besselk(-1, k(j) * RE));
end
% Set A_m0 for i = 2:M
for i = 2:M
A(i, 1) = 2 * cosh(k(1) * h) * (-1)^(i - 1) * k(1) * sinh(k(1) * b) / (b * (2 * k(1) * h + sinh(2 * k(1) * h)) * (k(1)^2 + m(i)^2)) * besselh(0, 1, k(1) * RE) / (k(1) * besselh(-1, 1, k(1)* RE));
end
% Set A_mn for i = 2:M, j = 2:N
for i = 2:M
for j = 2:N
A(i, j) = 2 * (-1)^(i - 1) * sin(k(j) * b) / (b * (2 * k(j) * h + sin(2 * k(j) * h)) * (k(j)^2 - m(i)^2)) * besselk(0, k(j) * RE) / (-k(j) * besselk(-1, k(j) * RE));
end
end
%%%%%%%%%%%%%%%%%%%%%%%% DefineMatrix B %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
B=zeros(N,M);
B(1,1) = 4 * sinh(k(1)*b) / (RE * log(RE/RI) *cosh(k(1)*h)); %set B_00
for j=2:M
B(1,j) = m(j)*((besselk(0,m(j)*RI)*besseli(-1, m(j)*RE) + besseli(0, m(j)*RI)*besselk(-1,m(j)*RE)))...
/(besseli(0, m(j)*RE)*besselk(0,m(j)*RI) - besseli(0,m(j)*RI)*besselk(0,m(j)*RE))*4*k(1)^2*(-1)^(j-1)*sinh(k(1)*b)/(cosh(k(1)*h)*(k(1)^2+m(j)^2)); %Set B_0m
end
for i=2:N
B(i,1)=4*sin(k(i)*b)/(RE*log(RE/RI)*cosh(k(i)*h));% Set B_n0
end
for i=2:N
for j=2:M
B(i,j)= m(j)*((besselk(0,m(j)*RI)*besseli(-1, m(j)*RE) + besseli(0, m(j)*RI)*besselk(-1,m(j)*RE)))...
/(besseli(0, m(j)*RE)*besselk(0,m(j)*RI) - besseli(0,m(j)*RI)*besselk(0,m(j)*RE))*4*k(i)^2*(-1)^(j-1)*sin(k(i)*b)/(cos(k(i)*h)*(k(i)^2-m(j)^2)); % Set B_nm
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%Define Matrix C%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
C=zeros(N,M);
C(1,1) = (-4 * sinh(k(1) * b) )/ (RE*log(RE / RI) *cosh(k(1)*h)) ; %set C_00
for j=2:M
C(1,j)=-1/(besseli(0, m(j)*RE)*besselk(0,m(j)*RI)-besseli(0,m(j)*RI)*besselk(0,m(j)*RE))*4*k(1)^2*(-1)^(j-1)*sinh(k(1)*b)/(cosh(k(1)*h)*(k(1)^2+m(j)^2)); %Set C_0m
end
for i=2:N
C(i,1)= (-4 * sin(k(i) *b)) / (RE * log(RE/ RI)* cos(k(i) * h)); %Set C_n0
end
for i=2:N
for j=2:M
C(i,j)=-1/(RE*(besseli(0, m(j)*RE)*besselk(0,m(j)*RI)-besseli(0,m(j)*RI)*besselk(0,m(j)*RE)))*4*k(i)^2*(-1)^(j-1)*sin(k(i)*b)/(cos(k(i)*h)*(k(i)^2-m(j)^2)); % Set C_nm
end
end
%%%%%%%%%%%%%%%%Define matrix D %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
D = zeros(M, N);
D(1,1) = (cosh(k(1) * h) * cosh(k(1) * b)) / (k(1) * b * (2 * k(1) * h + sinh(2 * k(1) * h))) * (besselj(0, k(1) * RI)) / ((k(1)*besselj(-1, k(1) * RI)));%D_00
for j=2:N
D(1,j)= (cos(k(j) * h) * sin(k(j) * b) )/ (k(j) * b * (2 * k(j) * h + sin(2 * k(j) * h))) * (besseli(0, k(j) * RI) )/ ((k(j)*besseli(-1, k(j) * RI))); %D_0n
end
for i=2:M
D(i,1)=(2 *k(1)* (-1)^(i-1)* cosh(k(1) * h) * sinh(k(1) * b) )/ ( b * (2 * k(1) * h + sinh(2 * k(1)* h)) * (k(1)^2 + m(i)^2)) *(besselj(0, k(1) * RI)) / ((k(1)*besselj(-1, k(1) * RI))); % Set D_m0
end
for i=2:M
for j=2:N
D(i,j)= (2 *k(j)* (-1)^(i-1)* cos(k(j) * h) * sin(k(j) * b) )/ (b * (2 * k(j) * h + sin(2 * k(j)* h)) * (k(j)^2 - m(i)^2)) * (besseli(0, k(j) * RI)) / ((k(j)*besseli(-1, k(j) * RI)));% D_mn
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Define matrix E %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
E=zeros(N,M);
E(1,1)=4*sinh(k(1)*b)/(RI*log(RE/RI)*cosh(k(1)*h)); % E_00
for j=2:M
E(1,j)= 1./(RI*(besseli(0, m(j)*RE)*besselk(0,m(j)*RI) - besseli(0,m(j)*RI)*besselk(0,m(j)*RE)))*4*k(1)^2*(-1)^(j-1)*sinh(k(1)*b)/((k(1)^2+m(j)^2)*cosh(k(1)*h)); % E_0m
end
for i=2:N
E(i,1)=4*sin(k(i)*b)/(RI*log(RE/RI)*cos(k(i)*h)); % E_n0
end
for i=2:N
for j=2:M
E(i,j)= 1./(RI*(besseli(0, m(j)*RE)*besselk(0,m(j)*RI) - besseli(0,m(j)*RI)*besselk(0,m(j)*RE)))*4*k(i)^2*(-1)^(j-1)*sin(k(i)*b)/((k(i)^2-m(j)^2)*cos(k(i)*h)); % E_nm
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Define matrix F %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%F%%%%%%%%%%%%%%%%%%%%%%
F=zeros(N,M);
F(1,1)=-4*sinh(k(1)*b)/(RI*log(RE/RI)*cosh(k(1)*h)); % F_00
for j=2:M
F(1,j)= -m(j)*(besseli(0,m(j)*RE)*besselk(-1, m(j)*RI) + besselk(0, m(j)*RE)*besseli(-1,m(j)*RI))...
/(besseli(0, m(j)*RE)*besselk(0,m(j)*RI) - besseli(0,m(j)*RI)*besselk(0,m(j)*RE))*4*k(1)^2*(-1)^(j-1)*sinh(k(1)*b)/((k(1)^2+m(j)^2)*cosh(k(1)*h)); %F_0m
end
for i=2:N
F(i,1)= -4*sin(k(i)*b)/(RI*log(RE/RI)*cos(k(i)*h)); % F_m0
end
for i=2:N
for j=2:M
F(i,j)=-m(j)*(besseli(0,m(j)*RE)*besselk(-1, m(j)*RI) + besselk(0, m(j)*RE)*besseli(-1,m(j)*RI))...
/(besseli(0, m(j)*RE)*besselk(0,m(j)*RI) - besseli(0,m(j)*RI)*besselk(0,m(j)*RE))*4*k(i)^2*(-1)^(j-1)*sin(k(i)*b)/((k(i)^2-m(j)^2)*cos(k(i)*h)); % F_nm
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % Define right-hand side vector G
G = zeros(2*M+2*N, 1); % Total length = M + N+ M+ N = 2M+2N
% Block 1: G (size M = 4)
for i = 1:M
if i == 1
G(i, 1) = (besselj(0, k(1)*RE) *sinh(k(1)*b))/((b*k(1))*cosh(k(1)*h)); % Z_0^l
else
G(i, 1) = (2*besselj(0, k(1)*RE) *k(1)*(-1)^(i-1)*sinh(k(1)*b))/(b*cosh(k(1)*h) *(k(1)^2 +m(i)^2)); %Z_m^l
end
end
% Block 2: Y (size N = 3)
for j = 1:N
if j == 1
G(j + M, 1) = -k(1)*besselj(-1, k(1)*RE) * (2*k(1)*h + sinh(2*k(1)*h)) /(cosh(k(1)*h)^2); % Y_0^l
else
G(j + M, 1) = 0; % Y_n^l
end
end
% Block 3: X (size M = 4)
for i = 1:M
G(i + M + N, 1) = 0; % X_0^l, X_m^l
end
% Block 4: W (size N = 3)
for j = 1:N
G(j + M + N + M, 1) = 0; % W_0^l, W_n^l
end
% Define identity and zero submatrices
I1 = zeros(M, M); % MxM identity
I2 = zeros(N, N); % NxN identity
I3 = zeros(M, M); % MxM identity
I4 = zeros(N, N); % N*N identity
for i = 1:M
I1(i,i) = 1;
I3(i,i) = 1;
end
for i = 1:N
I2(i,i) = 1;
I4(i,i) = 1;
end
%%%%%%%%%%%% Define zero submatrices%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
O1 = zeros(M, M); %
O2 = zeros(M, N); %
O3 = zeros(N, N); %
O4 = zeros(M, M); %
O5 = zeros(M, N); %
O6 = zeros(N, N); %
% Construct the matrix H while solving H X = G
H = [I1, -A, O1, O2; ...
-B, I2, -C, O3; ...
O4, O5, I3, -D; ...
-E, O6, -F, I4];
% % Solution of system of equations using inv or pinv (pscheudo inverse)
% we can use X_1=pinv(H)*G
X_1=pinv(H)*G;
% Extract the unknowns from the solution vector X_1
b_1 = zeros(1, M); % b0, b1, b2, b3
a_1 = zeros(1, N); % a0, a1, a2
c_1 = zeros(1, M); % c0, c1, c2, c3
d_1 = zeros(1, N); % d0, d1, d2
% Block 1: b_1 (size M = 4)
for i = 1:M
b_1(i) = X_1(i, 1);
end
% Block 2: a_1 (size N = 3)
for j = 1:N
a_1(j) = X_1(j + M, 1);
end
% Block 3: c_1 (size M = 4)
for i = 1:M
c_1(i) = X_1(i + M + N, 1);
end
% Block 4: d_1 (size N = 3)
for j = 1:N
d_1(j) = X_1(j + M + N + M, 1);
end
coef_A_0(idx)=abs((c_1(1) * (cosh(k(1) * h)^2))/(2*k(1)*h+sinh(2*k(1)*h))*k(1)*besselj(-1,k(1)*RI));
end
end
toc;
If you change
for idx = 1:length(nu)
to
for idx = 1:1
and display e.g. the matrix A you get from
% Initialize matrix A
A = zeros(M, N);
% Set A_00 (first element)
A(1, 1) = cosh(k(1) * h) * sinh(k(1) * b) / (k(1) * b * (2 * k(1) * h + sinh(2 * k(1)* h))) * besselh(0, 1, k(1) * RE) / (k(1) * besselh(-1, 1, k(1) * RE));
% Set A_0n for j = 2:N
for j = 2:N
A(1, j) = cos(k(j) * h) * sin(k(j) * b) / (k(j) * b * (2 * k(j) * h + sin(2 * k(j) * h))) * besselk(0, k(j) * RE) / (-k(j) * besselk(-1, k(j) * RE));
end
% Set A_m0 for i = 2:M
for i = 2:M
A(i, 1) = 2 * cosh(k(1) * h) * (-1)^(i - 1) * k(1) * sinh(k(1) * b) / (b * (2 * k(1) * h + sinh(2 * k(1) * h)) * (k(1)^2 + m(i)^2)) * besselh(0, 1, k(1) * RE) / (k(1) * besselh(-1, 1, k(1)* RE));
end
% Set A_mn for i = 2:M, j = 2:N
for i = 2:M
for j = 2:N
A(i, j) = 2 * (-1)^(i - 1) * sin(k(j) * b) / (b * (2 * k(j) * h + sin(2 * k(j) * h)) * (k(j)^2 - m(i)^2)) * besselk(0, k(j) * RE) / (-k(j) * besselk(-1, k(j) * RE));
end
end
, you will see that almost all elements are NaN values. I didn't check further which term leads to the undefined values.
Further, you generate k as a 2-dimensional matrix, but you use k in the other equations as if it were a 1-dimensional vector k(j). This cannot be correct.
Did you pass the introductory course MATLAB Onramp to learn the basics of the language ? It is free of costs and will help you at least with the programming part:
@Torsten okay thanks i will check it. But i end the loop for idx after denining all the matrices
Mathematical operations such as ​ and ​ are known as indeterminate forms and can result in NaN (Not a Number). Furthermore, any arithmetic operation involving NaN will also yield NaN. By examining coef_A_0 from the bottom up, you can trace NaN all the way from c_1(1) to X_1​. Since there is a pseudoinverse operation in X_1​ involving the matrix H, it is essential to verify the computation of each element in H.
%% Testing javeria's code
tic;
% N = 20; % Number of terms in region E and I
% M = 50; % Number of term in region B
N = 2; % Number of terms in region E and I
M = 2; % Number of term in region B
RE = 1.0; % Radius of external cylinder
ratios = [1.031,1.5, 2.0, 4.0]; % Ratios RE/RI you want to test
b = 2.0*RE; % The distance from seabed to the bottom of cylinder (h-d)=b
h = 4.0*RE; % Water depth.
% nu = linspace(0.01, 10, 100);% Loop over the range of nu
nu = [0.01, 0.02];
%% finding the root \lemda_m =m*pi/b%%%%%%
for j=1:M
m(j)=(pi*(j-1))/b;
end
for r = 1:length(ratios)
RI = RE / ratios(r); % Update RI based on the current ratio
% coef_A_0_all = []; % To store results for different ratios
% Preallocate wave numbers k
k = zeros(length(nu), N); % Preallocate k matrix
% Compute wave numbers k using the algorithm of Chamberlin & Porter
for idx = 1:length(nu) % Loop over the range of nu
a2=nu(idx)*h;
p=a2*(1-4*(1-(1+a2)*exp(-2*a2))/(2*a2+sinh(2*a2)))^(-0.25); % approximation for k_0 (Eq. 19)
k(idx,1) = p; % Store k_0
% Compute evanescent roots k1, ..., kN-1 using Eq. 26 + Eq. 23
for i2=1:N-1
b2=i2*pi-pi/2*tanh(2*a2/(i2*pi*pi));
for j=1:2
u=b2/(1-2*(b2*tan(b2)+a2)*sin(2*b2)/(a2*(sin(2*b2)+2*b2)))^0.5;
b2=u;
end
k(idx, i2 + 1) = b2; % Store the result for k_n
end
% Convert all to dimensional values by dividing by h
k(idx, :) = k(idx, :) / h;
% Make k1...kN imaginary (evanescent)
k(idx, 2:N) = k(idx, 2:N) * complex(0, 1);
% Compute the matrix A for different ratios
% Initialize matrix A
A = zeros(M, N);
% Set A_00 (first element)
A(1, 1) = cosh(k(1) * h) * sinh(k(1) * b) / (k(1) * b * (2 * k(1) * h + sinh(2 * k(1)* h))) * besselh(0, 1, k(1) * RE) / (k(1) * besselh(-1, 1, k(1) * RE));
% Set A_0n for j = 2:N
for j = 2:N
A(1, j) = cos(k(j) * h) * sin(k(j) * b) / (k(j) * b * (2 * k(j) * h + sin(2 * k(j) * h))) * besselk(0, k(j) * RE) / (-k(j) * besselk(-1, k(j) * RE));
end
% Set A_m0 for i = 2:M
for i = 2:M
A(i, 1) = 2 * cosh(k(1) * h) * (-1)^(i - 1) * k(1) * sinh(k(1) * b) / (b * (2 * k(1) * h + sinh(2 * k(1) * h)) * (k(1)^2 + m(i)^2)) * besselh(0, 1, k(1) * RE) / (k(1) * besselh(-1, 1, k(1)* RE));
end
% Set A_mn for i = 2:M, j = 2:N
for i = 2:M
for j = 2:N
A(i, j) = 2 * (-1)^(i - 1) * sin(k(j) * b) / (b * (2 * k(j) * h + sin(2 * k(j) * h)) * (k(j)^2 - m(i)^2)) * besselk(0, k(j) * RE) / (-k(j) * besselk(-1, k(j) * RE));
end
end
%%%%%%%%%%%%%%%%%%%%%%%% DefineMatrix B %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
B=zeros(N,M);
B(1,1) = 4 * sinh(k(1)*b) / (RE * log(RE/RI) *cosh(k(1)*h)); %set B_00
for j=2:M
B(1,j) = m(j)*((besselk(0,m(j)*RI)*besseli(-1, m(j)*RE) + besseli(0, m(j)*RI)*besselk(-1,m(j)*RE)))...
/(besseli(0, m(j)*RE)*besselk(0,m(j)*RI) - besseli(0,m(j)*RI)*besselk(0,m(j)*RE))*4*k(1)^2*(-1)^(j-1)*sinh(k(1)*b)/(cosh(k(1)*h)*(k(1)^2+m(j)^2)); %Set B_0m
end
for i=2:N
B(i,1)=4*sin(k(i)*b)/(RE*log(RE/RI)*cosh(k(i)*h));% Set B_n0
end
for i=2:N
for j=2:M
B(i,j)= m(j)*((besselk(0,m(j)*RI)*besseli(-1, m(j)*RE) + besseli(0, m(j)*RI)*besselk(-1,m(j)*RE)))...
/(besseli(0, m(j)*RE)*besselk(0,m(j)*RI) - besseli(0,m(j)*RI)*besselk(0,m(j)*RE))*4*k(i)^2*(-1)^(j-1)*sin(k(i)*b)/(cos(k(i)*h)*(k(i)^2-m(j)^2)); % Set B_nm
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%Define Matrix C%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
C=zeros(N,M);
C(1,1) = (-4 * sinh(k(1) * b) )/ (RE*log(RE / RI) *cosh(k(1)*h)) ; %set C_00
for j=2:M
C(1,j)=-1/(besseli(0, m(j)*RE)*besselk(0,m(j)*RI)-besseli(0,m(j)*RI)*besselk(0,m(j)*RE))*4*k(1)^2*(-1)^(j-1)*sinh(k(1)*b)/(cosh(k(1)*h)*(k(1)^2+m(j)^2)); %Set C_0m
end
for i=2:N
C(i,1)= (-4 * sin(k(i) *b)) / (RE * log(RE/ RI)* cos(k(i) * h)); %Set C_n0
end
for i=2:N
for j=2:M
C(i,j)=-1/(RE*(besseli(0, m(j)*RE)*besselk(0,m(j)*RI)-besseli(0,m(j)*RI)*besselk(0,m(j)*RE)))*4*k(i)^2*(-1)^(j-1)*sin(k(i)*b)/(cos(k(i)*h)*(k(i)^2-m(j)^2)); % Set C_nm
end
end
%%%%%%%%%%%%%%%%Define matrix D %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
D = zeros(M, N);
D(1,1) = (cosh(k(1) * h) * cosh(k(1) * b)) / (k(1) * b * (2 * k(1) * h + sinh(2 * k(1) * h))) * (besselj(0, k(1) * RI)) / ((k(1)*besselj(-1, k(1) * RI)));%D_00
for j=2:N
D(1,j)= (cos(k(j) * h) * sin(k(j) * b) )/ (k(j) * b * (2 * k(j) * h + sin(2 * k(j) * h))) * (besseli(0, k(j) * RI) )/ ((k(j)*besseli(-1, k(j) * RI))); %D_0n
end
for i=2:M
D(i,1)=(2 *k(1)* (-1)^(i-1)* cosh(k(1) * h) * sinh(k(1) * b) )/ ( b * (2 * k(1) * h + sinh(2 * k(1)* h)) * (k(1)^2 + m(i)^2)) *(besselj(0, k(1) * RI)) / ((k(1)*besselj(-1, k(1) * RI))); % Set D_m0
end
for i=2:M
for j=2:N
D(i,j)= (2 *k(j)* (-1)^(i-1)* cos(k(j) * h) * sin(k(j) * b) )/ (b * (2 * k(j) * h + sin(2 * k(j)* h)) * (k(j)^2 - m(i)^2)) * (besseli(0, k(j) * RI)) / ((k(j)*besseli(-1, k(j) * RI)));% D_mn
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Define matrix E %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
E=zeros(N,M);
E(1,1)=4*sinh(k(1)*b)/(RI*log(RE/RI)*cosh(k(1)*h)); % E_00
for j=2:M
E(1,j)= 1./(RI*(besseli(0, m(j)*RE)*besselk(0,m(j)*RI) - besseli(0,m(j)*RI)*besselk(0,m(j)*RE)))*4*k(1)^2*(-1)^(j-1)*sinh(k(1)*b)/((k(1)^2+m(j)^2)*cosh(k(1)*h)); % E_0m
end
for i=2:N
E(i,1)=4*sin(k(i)*b)/(RI*log(RE/RI)*cos(k(i)*h)); % E_n0
end
for i=2:N
for j=2:M
E(i,j)= 1./(RI*(besseli(0, m(j)*RE)*besselk(0,m(j)*RI) - besseli(0,m(j)*RI)*besselk(0,m(j)*RE)))*4*k(i)^2*(-1)^(j-1)*sin(k(i)*b)/((k(i)^2-m(j)^2)*cos(k(i)*h)); % E_nm
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Define matrix F %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%F%%%%%%%%%%%%%%%%%%%%%%
F=zeros(N,M);
F(1,1)=-4*sinh(k(1)*b)/(RI*log(RE/RI)*cosh(k(1)*h)); % F_00
for j=2:M
F(1,j)= -m(j)*(besseli(0,m(j)*RE)*besselk(-1, m(j)*RI) + besselk(0, m(j)*RE)*besseli(-1,m(j)*RI))...
/(besseli(0, m(j)*RE)*besselk(0,m(j)*RI) - besseli(0,m(j)*RI)*besselk(0,m(j)*RE))*4*k(1)^2*(-1)^(j-1)*sinh(k(1)*b)/((k(1)^2+m(j)^2)*cosh(k(1)*h)); %F_0m
end
for i=2:N
F(i,1)= -4*sin(k(i)*b)/(RI*log(RE/RI)*cos(k(i)*h)); % F_m0
end
for i=2:N
for j=2:M
F(i,j)=-m(j)*(besseli(0,m(j)*RE)*besselk(-1, m(j)*RI) + besselk(0, m(j)*RE)*besseli(-1,m(j)*RI))...
/(besseli(0, m(j)*RE)*besselk(0,m(j)*RI) - besseli(0,m(j)*RI)*besselk(0,m(j)*RE))*4*k(i)^2*(-1)^(j-1)*sin(k(i)*b)/((k(i)^2-m(j)^2)*cos(k(i)*h)); % F_nm
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % Define right-hand side vector G
G = zeros(2*M+2*N, 1); % Total length = M + N+ M+ N = 2M+2N
% Block 1: G (size M = 4)
for i = 1:M
if i == 1
G(i, 1) = (besselj(0, k(1)*RE) *sinh(k(1)*b))/((b*k(1))*cosh(k(1)*h)); % Z_0^l
else
G(i, 1) = (2*besselj(0, k(1)*RE) *k(1)*(-1)^(i-1)*sinh(k(1)*b))/(b*cosh(k(1)*h) *(k(1)^2 +m(i)^2)); %Z_m^l
end
end
% Block 2: Y (size N = 3)
for j = 1:N
if j == 1
G(j + M, 1) = -k(1)*besselj(-1, k(1)*RE) * (2*k(1)*h + sinh(2*k(1)*h)) /(cosh(k(1)*h)^2); % Y_0^l
else
G(j + M, 1) = 0; % Y_n^l
end
end
% Block 3: X (size M = 4)
for i = 1:M
G(i + M + N, 1) = 0; % X_0^l, X_m^l
end
% Block 4: W (size N = 3)
for j = 1:N
G(j + M + N + M, 1) = 0; % W_0^l, W_n^l
end
% Define identity and zero submatrices
I1 = zeros(M, M); % MxM identity
I2 = zeros(N, N); % NxN identity
I3 = zeros(M, M); % MxM identity
I4 = zeros(N, N); % N*N identity
for i = 1:M
I1(i,i) = 1;
I3(i,i) = 1;
end
for i = 1:N
I2(i,i) = 1;
I4(i,i) = 1;
end
%%%%%%%%%%%% Define zero submatrices%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
O1 = zeros(M, M); %
O2 = zeros(M, N); %
O3 = zeros(N, N); %
O4 = zeros(M, M); %
O5 = zeros(M, N); %
O6 = zeros(N, N); %
% Construct the matrix H while solving H X = G
H = [I1, -A, O1, O2; ...
-B, I2, -C, O3; ...
O4, O5, I3, -D; ...
-E, O6, -F, I4]
% % Solution of system of equations using inv or pinv (pscheudo inverse)
% we can use X_1=pinv(H)*G
X_1 = pinv(H)*G
% Extract the unknowns from the solution vector X_1
b_1 = zeros(1, M); % b0, b1, b2, b3
a_1 = zeros(1, N); % a0, a1, a2
c_1 = zeros(1, M); % c0, c1, c2, c3
d_1 = zeros(1, N); % d0, d1, d2
% Block 1: b_1 (size M = 4)
for i = 1:M
b_1(i) = X_1(i, 1);
end
% Block 2: a_1 (size N = 3)
for j = 1:N
a_1(j) = X_1(j + M, 1);
end
% Block 3: c_1 (size M = 4)
for i = 1:M
c_1(i) = X_1(i + M + N, 1);
end
% Block 4: d_1 (size N = 3)
for j = 1:N
d_1(j) = X_1(j + M + N + M, 1);
end
term1(idx) = c_1(1);
coef_A_0(idx) = abs( ( c_1(1)*cosh(k(1)*h)^2 )/( 2*k(1)*h + sinh(2*k(1)*h) ) * k(1)*besselj(-1, k(1)*RI) );
end
end
H =
1.0e+04 * 0.0001 + 0.0000i 0.0000 + 0.0000i 0.0004 + 0.0002i NaN + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i -0.0000 - 0.0000i NaN + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i -0.0013 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i 0.0000 + 0.0000i 0.0013 + 0.0000i -0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i 0.0000 + 0.0000i 1.0152 + 0.0000i NaN + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i -0.0002 + 0.0000i NaN + 0.0000i -0.0013 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0013 + 0.0000i -0.0000 + 0.0000i 0.0001 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i
X_1 =
NaN + NaNi NaN + NaNi NaN + NaNi NaN + NaNi NaN + NaNi NaN + NaNi NaN + NaNi NaN + NaNi
H =
1.0e+04 * 0.0001 + 0.0000i 0.0000 + 0.0000i 0.0004 + 0.0002i 0.0002 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i -0.0000 - 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i -0.0013 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i 0.0000 + 0.0000i 0.0013 + 0.0000i -0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i -0.0018 + 0.0000i -0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i 0.0020 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i 0.0000 + 0.0000i 1.0152 + 0.0000i -0.0344 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i -0.0002 + 0.0000i -0.0001 + 0.0000i -0.0013 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0013 + 0.0000i -0.0000 + 0.0000i 0.0001 + 0.0000i 0.0000 + 0.0000i -0.0020 + 0.0000i -0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0020 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i
X_1 =
-0.3709 + 0.0006i -0.0839 + 0.0001i 0.0009 + 0.0000i 0.5663 - 0.0010i -0.3708 + 0.0006i -0.0074 + 0.0000i -0.0001 + 0.0000i -0.0050 + 0.0000i
H =
1.0e+04 * 0.0001 + 0.0000i 0.0000 + 0.0000i 0.0004 + 0.0002i NaN + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i -0.0000 - 0.0000i NaN + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i -0.0001 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i -0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i 0.0000 + 0.0000i 1.4772 + 0.0000i NaN + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i -0.0003 + 0.0000i NaN + 0.0000i -0.0001 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i -0.0000 + 0.0000i 0.0001 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i
X_1 =
NaN + NaNi NaN + NaNi NaN + NaNi NaN + NaNi NaN + NaNi NaN + NaNi NaN + NaNi NaN + NaNi
H =
1.0e+04 * 0.0001 + 0.0000i 0.0000 + 0.0000i 0.0004 + 0.0002i 0.0002 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i -0.0000 - 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i -0.0001 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i -0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i -0.0001 + 0.0000i -0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i 0.0001 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i 0.0000 + 0.0000i 1.4772 + 0.0000i -0.0500 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i -0.0003 + 0.0000i -0.0002 + 0.0000i -0.0001 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i -0.0000 + 0.0000i 0.0001 + 0.0000i 0.0000 + 0.0000i -0.0002 + 0.0000i -0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0002 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i
X_1 =
1.3503 - 0.0025i 0.0206 - 0.0000i 0.0009 + 0.0000i -0.1566 + 0.0003i 1.3504 - 0.0025i -0.0003 + 0.0000i -0.0001 + 0.0000i -0.0000 + 0.0000i
H =
1.0e+04 * 0.0001 + 0.0000i 0.0000 + 0.0000i 0.0004 + 0.0002i NaN + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i -0.0000 - 0.0000i NaN + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i -0.0001 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i -0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i 0.0000 + 0.0000i 1.9698 + 0.0000i NaN + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i -0.0004 + 0.0000i NaN + 0.0000i -0.0001 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i -0.0000 + 0.0000i 0.0001 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i
X_1 =
NaN + NaNi NaN + NaNi NaN + NaNi NaN + NaNi NaN + NaNi NaN + NaNi NaN + NaNi NaN + NaNi
H =
1.0e+04 * 0.0001 + 0.0000i 0.0000 + 0.0000i 0.0004 + 0.0002i 0.0002 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i -0.0000 - 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i -0.0001 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i -0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i -0.0001 + 0.0000i -0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i 0.0001 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i 0.0000 + 0.0000i 1.9698 + 0.0000i -0.0667 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i -0.0004 + 0.0000i -0.0003 + 0.0000i -0.0001 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i -0.0000 + 0.0000i 0.0001 + 0.0000i 0.0000 + 0.0000i -0.0002 + 0.0000i -0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0002 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i
X_1 =
1.1657 - 0.0022i 0.0094 - 0.0000i 0.0010 + 0.0000i -0.0791 + 0.0002i 1.1657 - 0.0022i -0.0004 + 0.0000i -0.0001 + 0.0000i -0.0000 + 0.0000i
H =
1.0e+04 * 0.0001 + 0.0000i 0.0000 + 0.0000i 0.0004 + 0.0002i NaN + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i -0.0000 - 0.0000i NaN + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i -0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i -0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i 0.0000 + 0.0000i 3.9398 + 0.0000i NaN + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i -0.0008 + 0.0000i NaN + 0.0000i -0.0001 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i -0.0000 + 0.0000i 0.0001 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i
X_1 =
NaN + NaNi NaN + NaNi NaN + NaNi NaN + NaNi NaN + NaNi NaN + NaNi NaN + NaNi NaN + NaNi
H =
1.0e+04 * 0.0001 + 0.0000i 0.0000 + 0.0000i 0.0004 + 0.0002i 0.0002 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i -0.0000 - 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i -0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i -0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i -0.0000 + 0.0000i -0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i 0.0000 + 0.0000i 3.9398 + 0.0000i -0.1334 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i -0.0008 + 0.0000i -0.0006 + 0.0000i -0.0001 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i -0.0000 + 0.0000i 0.0001 + 0.0000i 0.0000 + 0.0000i -0.0002 + 0.0000i -0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0002 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0001 + 0.0000i
X_1 =
1.0632 - 0.0021i 0.0032 - 0.0000i 0.0010 + 0.0000i -0.0361 + 0.0001i 1.0632 - 0.0021i -0.0004 + 0.0000i -0.0000 + 0.0000i -0.0000 + 0.0000i
toc;
Elapsed time is 0.332074 seconds.
disp(term1) % a term "c_1(1)" in coef_A_0
NaN + NaNi 1.0632 - 0.0021i
@Torsten @Sam Chak Yes i know 0/0 is undefine. But when my k roots becomes imiginary then it give me NaN results. i.e when my n=2:N. But according to chemberlin and port k_n is imignary roots.
@Sam Chak @Sam Chak i take nu(as a single value ) and run my code for this matrix A and it doesnot give me any error. So now can you please edit my code for range of nu values. I know basics of matlab and yes i take courses also. But when i give loop for nu then it give me NaN.
% % This is the code for finding roots of the dispersion relation
% % K = k * tanh(kh)
% % here K = nu^2/g, nu is angular frequency and h is water depth
% function [ k1 ] = dispersion( nu,h,N)
RE = 1.0; % Radius of external cylinder
ratios = [1.031, 1.5, 2.0, 4.0]; % Ratios RE/RI you want to test
b = 2.0 * RE; % Distance from seabed to the bottom of cylinder (h-d)=b
h = 4.0 * RE; % Water depth
N=3; % how many roots you need? Change your N to get roots
M = 4;% Change your M according to u
nu = 0.1; % angular frequency
h=4.0; % water depth
%% finding the root \lemda_m =m*pi/b%%%%%%
for j=1:M
m(j)=(pi*(j-1))/b;
end
k = zeros(1, N);
% % Use the algorithm of Chamberlin & Porter
a2=nu*h;
p=a2*(1-4*(1-(1+a2)*exp(-2*a2))/(2*a2+sinh(2*a2)))^(-0.25); % approximation for k_0 (Eq. 19)
k(1)=p;
for i2=1:N-1
b2=i2*pi-pi/2*tanh(2*a2/(i2*pi*pi));%aproximation for k_n (Eq. 26)
for j=1:2
u=b2/(1 - (2*(b2*tan(b2) + a2)*sin(2*b2))/(a2*(sin(2*b2)+2*b2)))^0.5; % From article (Eq. 23), an upper bound for k_n, iterated to refine the roots
b2=u;
end
k(i2+1)=b2;
end
k=k/h;
k(:,2:N)=k(:,2:N)*complex(0,1); % print all roots where first one is real and remaining ones are purely imaginary
for r = 1:length(ratios)
RI = RE / ratios(r); % Update RI based on the current ratio
% Example: Initialize matrix A as a zero matrix of size NxN
A = zeros(M, N);
% Set A_00 (first element)
A(1, 1) = cosh(k(1) * h) * sinh(k(1) * b) / (k(1) * b * (2*k(1)*h + sinh(2 * k(1)* h))) * besselh(0, 1, k(1) * RE) / (k(1) * besselh(-1, 1, k(1) * RE));
% Set A_0n for j = 2:N
for j = 2:N
A(1, j) = cos(k(j) * h) * sin(k(j) * b) / (k(j) * b * (2*k(j)*h + sin(2*k(j) * h))) * besselk(0, k(j) * RE) / (-k(j) * besselk(-1, k(j) * RE));
end
% Set A_m0 for i = 2:M
for i = 2:M
A(i, 1) = 2 * cosh(k(1) * h) * (-1)^(i - 1) * k(1) * sinh(k(1) * b) / (b*(2 * k(1) * h + sinh(2 * k(1) * h)) * (k(1)^2 + m(i)^2)) * besselh(0, 1, k(1) * RE) / (k(1) * besselh(-1, 1, k(1)* RE));
end
% Set A_mn for i = 2:M, j = 2:N
for i = 2:M
for j = 2:N
A(i, j) = 2 * (-1)^(i - 1) * sin(k(j) * b) / (b * (2 * k(j) * h + sin(2 * k(j) * h)) * (k(j)^2 - m(i)^2)) * besselk(0, k(j) * RE) / (-k(j) * besselk(-1, k(j) * RE));
end
end
% After calculating matrix A, you can process or print it as required
fprintf('Matrix A for ratio %.3f and RI %.3f:\n', ratios(r), RI);
disp(A);
end
Matrix A for ratio 1.031 and RI 0.970:
-0.7334 - 0.5851i 0.0644 + 0.0336i 0.0041 + 0.0012i 0.0169 + 0.0135i -0.0016 + 0.0031i -0.0000 + 0.0000i -0.0043 - 0.0034i 0.0005 - 0.0009i 0.0000 - 0.0000i 0.0019 + 0.0015i -0.0002 + 0.0004i -0.0000 + 0.0000i
Matrix A for ratio 1.500 and RI 0.667:
-0.7334 - 0.5851i 0.0644 + 0.0336i 0.0041 + 0.0012i 0.0169 + 0.0135i -0.0016 + 0.0031i -0.0000 + 0.0000i -0.0043 - 0.0034i 0.0005 - 0.0009i 0.0000 - 0.0000i 0.0019 + 0.0015i -0.0002 + 0.0004i -0.0000 + 0.0000i
Matrix A for ratio 2.000 and RI 0.500:
-0.7334 - 0.5851i 0.0644 + 0.0336i 0.0041 + 0.0012i 0.0169 + 0.0135i -0.0016 + 0.0031i -0.0000 + 0.0000i -0.0043 - 0.0034i 0.0005 - 0.0009i 0.0000 - 0.0000i 0.0019 + 0.0015i -0.0002 + 0.0004i -0.0000 + 0.0000i
Matrix A for ratio 4.000 and RI 0.250:
-0.7334 - 0.5851i 0.0644 + 0.0336i 0.0041 + 0.0012i 0.0169 + 0.0135i -0.0016 + 0.0031i -0.0000 + 0.0000i -0.0043 - 0.0034i 0.0005 - 0.0009i 0.0000 - 0.0000i 0.0019 + 0.0015i -0.0002 + 0.0004i -0.0000 + 0.0000i
% % This is the code for finding roots of the dispersion relation
% % K = k * tanh(kh)
% % here K = nu^2/g, nu is angular frequency and h is water depth
% function [ k1 ] = dispersion( nu,h,N)
RE = 1.0; % Radius of external cylinder
ratios = [1.031, 1.5, 2.0, 4.0]; % Ratios RE/RI you want to test
b = 2.0 * RE; % Distance from seabed to the bottom of cylinder (h-d)=b
h = 4.0 * RE; % Water depth
N=3; % how many roots you need? Change your N to get roots
M = 4;% Change your M according to u
Nu = [0.1;0.2]; % angular frequency
h=4.0; % water depth
%% finding the root \lemda_m =m*pi/b%%%%%%
for j=1:M
m(j)=(pi*(j-1))/b;
end
for nn = 1:numel(Nu)
nu = Nu(nn);
k = zeros(1, N);
% % Use the algorithm of Chamberlin & Porter
a2=nu*h;
p=a2*(1-4*(1-(1+a2)*exp(-2*a2))/(2*a2+sinh(2*a2)))^(-0.25); % approximation for k_0 (Eq. 19)
k(1)=p;
for i2=1:N-1
b2=i2*pi-pi/2*tanh(2*a2/(i2*pi*pi));%aproximation for k_n (Eq. 26)
for j=1:2
u=b2/(1 - (2*(b2*tan(b2) + a2)*sin(2*b2))/(a2*(sin(2*b2)+2*b2)))^0.5; % From article (Eq. 23), an upper bound for k_n, iterated to refine the roots
b2=u;
end
k(i2+1)=b2;
end
k=k/h;
k(:,2:N)=k(:,2:N)*complex(0,1); % print all roots where first one is real and remaining ones are purely imaginary
for r = 1:length(ratios)
RI = RE / ratios(r); % Update RI based on the current ratio
% Example: Initialize matrix A as a zero matrix of size NxN
A = zeros(M, N);
% Set A_00 (first element)
A(1, 1) = cosh(k(1) * h) * sinh(k(1) * b) / (k(1) * b * (2*k(1)*h + sinh(2 * k(1)* h))) * besselh(0, 1, k(1) * RE) / (k(1) * besselh(-1, 1, k(1) * RE));
% Set A_0n for j = 2:N
for j = 2:N
A(1, j) = cos(k(j) * h) * sin(k(j) * b) / (k(j) * b * (2*k(j)*h + sin(2*k(j) * h))) * besselk(0, k(j) * RE) / (-k(j) * besselk(-1, k(j) * RE));
end
% Set A_m0 for i = 2:M
for i = 2:M
A(i, 1) = 2 * cosh(k(1) * h) * (-1)^(i - 1) * k(1) * sinh(k(1) * b) / (b*(2 * k(1) * h + sinh(2 * k(1) * h)) * (k(1)^2 + m(i)^2)) * besselh(0, 1, k(1) * RE) / (k(1) * besselh(-1, 1, k(1)* RE));
end
% Set A_mn for i = 2:M, j = 2:N
for i = 2:M
for j = 2:N
A(i, j) = 2 * (-1)^(i - 1) * sin(k(j) * b) / (b * (2 * k(j) * h + sin(2 * k(j) * h)) * (k(j)^2 - m(i)^2)) * besselk(0, k(j) * RE) / (-k(j) * besselk(-1, k(j) * RE));
end
end
% After calculating matrix A, you can process or print it as required
fprintf('Matrix A for ratio %.3f and RI %.3f:\n', ratios(r), RI);
disp(A);
end
end

Accedi per commentare.

@Torsten @Sam Chak I need some help regarding my research I am a Phd student. I derive analytical equations for my system to find the unknown coefficient however my results are not corrected. If i provide my analytical expression can you do help me in coding part. Or if you have mathematics background can you also help me in analytical part.
Thanks

Categorie

Scopri di più su Mathematics in Centro assistenza e File Exchange

Richiesto:

il 25 Mar 2025

Risposto:

il 16 Apr 2025

Community Treasure Hunt

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

Start Hunting!

Translated by