- https://www.mathworks.com/help/matlab/ref/integral.html?s_tid=doc_ta#btbbkta-3
- https://www.mathworks.com/help/matlab/matlab_prog/creating-a-function-handle.html
Converting Equation of Two Variables Into Matrix Elements - ERROR: Invalid MATLAB indexing or symbolic function definition
5 views (last 30 days)
Show older comments
Hello everyone,
I am trying to output a square matrix that is N x M in size, where N = M. In my case, N = 3. So this is a "3x3" matrix
I have a variable that's called "Kernel". This variable is calculated as an equation in terms of "z" and "z_prime".
My values in z_prime will vary from L / 2N to (2N-1)*(L/2N) , in steps of "L/N".
My "z" variable will be used as an "integration variable" as seen below.
I am trying to calculate an output called the "Z Matrix" which will integrate the "Kernal" variable across specific ranges.
I attached a .pdf file to clarify my end goal.
See the .pdf file attached:
Z_Matrix_Output.pdf
My Integration Limits for "z" will be going from:
z_start = 0 : L/N : (N-1) * (L/N)
z_end = L/N : L/N : L
As of right now, I'm getting the error:
Invalid MATLAB indexing or symbolic function definition
This is after I tried using "function handles" in my code.
In my attempt, I have tried to use for loops and created function handles to make it easier to generate this matrix.However, I didn't end up with a "N X M" matrix, I just ended up with a "1 X N" matrix.
I know that there is a way to use "nested" for loops to generate a square matrix of an "N X M" size. However, I wasn't quite sure on how to implement that in MATLAB for my case.
I have tried to look through different questions/answers on the Mathworks Forum regarding converting equations of two variables into "matrix elements". However, I wasn't able to find anything that was relevant to my case.
I attached my MATLAB Code for reference.
See MATLAB .m file attached:
Z_Calculation.m
0 Comments
Accepted Answer
Shashi Kiran
on 30 Jul 2024
Hi Ammar,
The code looks interesting.
I understand that you are encountering errors while using MATLAB's 'integral' function for integration.
According to the documentation, the 'integral' function requires the input argument 'integrand' to be a function handle.
Based on the code and theory you have provided,
The Kernel and function integral can be defined as below.
% Kernel function
Kernel = F_1 * F_2 * (F_3 * F_4 + F_5);
% Convert Kernel to a function handle
Kernel_handle = matlabFunction(Kernel, 'Vars', [z, z_prime]);
% Define the integrand as a function handle
integrand = @(z) Kernel_handle(z, loop_z_prime);
% Integrate using the defined function handle
Z(i,j) = integral(integrand, loop_lower_z, loop_upper_z);
Below is complete implementation of your code using the above arguments
%% Starting Timer and Clearing Plots/Command Line
tic
close all; clc;
%% Fixed Variables and Parameters
% Value of j
j = 1i;
% Speed of light = c = 3*10^8 m/s
c = 3*10^8;
% Frequency = 300 MHz = 300*10^6 Hz = 3*10^8 Hz
f = 3*10^8;
% Value of lambda = c / f
lambda = c/f;
% Wavenumber value = k
k = (2*pi)/lambda;
% Intrinsic Impedance Value = eta
eta = 120*pi;
% Radius of the wire
a = 0.005*lambda;
% Length of the dipole antenna
L = 0.5*lambda;
% Number of segments
N = 3;
% Number of columns
M = N;
% Declaring "z" and "z_prime" as symbolic variables
syms z z_prime;
%% Calculating the Kernel Factor
% R equation
R = sqrt(a^2 + (z - z_prime)^2);
% Factor_1 = (-j*eta) / k
F_1 = (-j*eta) / k;
% Factor_2 = (exp(-j*k*R)) / (4*pi*(R)^5)
F_2 = exp(-j*k*R) / (4*pi*(R)^5);
% Factor_3 = 1 + jkR
F_3 = 1 + (j*k*R);
% Factor_4 = 2*R^2 - 3*a^2
F_4 = (2*R^2) - (3*a^2);
% Factor_5 = (k*a*R)^2
F_5 = (k*a*R)^2;
% Kernel function
Kernel = F_1 * F_2 * (F_3 * F_4 + F_5);
% Convert Kernel to a function handle
Kernel_handle = matlabFunction(Kernel, 'Vars', [z, z_prime]);
% Upper and lower z integration boundaries
delta_z = L / N;
upper_z = 0 : delta_z : L - delta_z;
lower_z = delta_z : delta_z : L;
% Start and stop values for z_prime
z_prime_start = L / (2*N);
delta_z_prime = L / N;
z_prime_end = L - z_prime_start;
z_prime = z_prime_start : delta_z_prime : z_prime_end;
% Pre-allocating the Z Matrix into an N x N Matrix
Z = zeros(N, N);
for i = 1:N
for j = 1:M
loop_z_prime = z_prime(i);
loop_upper_z = upper_z(j);
loop_lower_z = lower_z(j);
% Define the integrand as a function handle
integrand = @(z) Kernel_handle(z, loop_z_prime);
% Integrate using the defined function handle
Z(i,j) = integral(integrand, loop_lower_z, loop_upper_z);
end
end
toc
disp(Z)
Refer to the following references for further help.
Hope this answers your query.
More Answers (1)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!