app designer error: Attempt to add "theta" to a static workspace.

2 views (last 30 days)
Hello, i wrote a code and it works while running the script. but when i use it in an app this error shows up Attempt to add "theta" to a static workspace. can somebody help me out. thanks
The code:
%% Constants and parameters
J1 = 1; % Polarization of outer magnet
J2 = 1; % Polarization of inner magnet
u0 = pi*4e-7; % Magnetic constant
%% Position vectors, define appropriately
r = [0.25, 0.3, 0.20 , 0.24]; % radius of magnets (outer to inner)
z = [0.05, 0.15, 0.08, 0.18]; % z cordinate of magnets (outer to inner)
%% main function g
g = @(a, b, c) computeG(a, b, c);
% Initialize F_Z
F_Z = 0;
% Compute F_Z based on the given formula
for i = 1:2
for k = 1:2
for j = 3:4
for l = 3:4
deltaZ = z(k) - z(l);
ri2 = r(i)^2;
rj2 = r(j)^2;
Fijkl = r(i) * r(j) * g(deltaZ, ri2+rj2+deltaZ^2, -2 * r(i) * r(j));
power = 1 + i + j + k + l;
F_Z = F_Z + ((-1)^power) * Fijkl;
end
end
end
end
F_Z = ((J1 * J2) / (2 * u0)) * F_Z;
%% Define the function computeG
function result = computeG(a, b, c)
A = (a^2 - b) / c * pi + sqrt(c^2 - (a^2 - b)^2) / c *(log((-16 * c^2) / ((c^2 - (a^2 - b)^2)^1.5)) + log(c^2 / ((c^2 - (a^2 - b)^2)^1.5)));
S = computeS(a, b, c);
result = A + S;
end
%% Define the function computeS
function S = computeS(a, b, c)
% Define constants
epsilon = c / (c - b);
beta = (b + c) / (b - c);
mu = c / (b + c);
% Calculate terms for S
term1 = (2 * 1i * a) / (c * sqrt(b + c)) * ...
((b + c) * ellipticE(asin(sqrt(beta)), beta^(-1)) - c * ellipticF(asin(sqrt(beta)), beta^(-1)));
term2 = (2 * a) / (c * sqrt(b - c) * sqrt(epsilon)) * ...
(c / sqrt(mu) * ellipticE1(1 / beta) - c * sqrt(mu) * ellipticK(1 / beta));
term3 = sqrt(epsilon * (1 / beta)) * ...
((b - a^2) * ellipticK(2 * mu) + (a^2 - b + c) * ellipticPi(2 * c / (c + b - a^2), 2 * mu));
% Sum all terms
S = term1 + term2 + term3;
end
% Elliptic integral functions using symbolic math toolbox
function val = ellipticK(m)
syms theta;
val = double(int(1 / sqrt(1 - m * sin(theta)^2), theta, 0, pi/2));
end
function val = ellipticF(phi, m)
syms theta;
val = double(int(1 / sqrt(1 - m * sin(theta)^2), theta, 0, phi));
end
function val = ellipticE(phi, m)
syms theta;
val = double(int(sqrt(1 - m * sin(theta)^2), theta, 0, phi));
end
function val = ellipticE1(m)
syms theta;
val = double(int(sqrt(1 - m * sin(theta)^2), theta, 0, pi/2));
end
function val = ellipticPi(n, m)
syms theta;
val = double(int(1 / (sqrt(1 - n * sin(theta)^2) * sqrt(1 - m * sin(theta)^2)), theta, 0, pi/2));
end

Accepted Answer

Walter Roberson
Walter Roberson on 19 Aug 2024
Replace
syms theta
with
theta = sym('theta');
Note:
If you are trying to compile this code, then you will not be able to do so: nothing about the Symbolic Toolbox can be compiled.
  6 Comments
mohammadreza j
mohammadreza j on 19 Aug 2024
Thanks for instant answering Walter. Can you please explain this more cleary this "Eventually you would get down to integrate() calls, which you would code using 'Arrayvalued', true" in an example.
Thanks
Walter Roberson
Walter Roberson on 19 Aug 2024
function val = ellipticK(m)
val = integrate(@(theta) 1 ./ sqrt(1 - m * sin(theta).^2), 0, pi/2, 'ArrayValued', true);
end

Sign in to comment.

More Answers (0)

Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!