How to get numerical equation for my code?

6 views (last 30 days)
I have the next matlab's code:
function [Y1] = roots_2021a(q)
syms r
Y=airy(1,-r)-q*airy(-r);
x=0:0.1:20;
y=double(subs(Y,r,x));
d=diff(y>0);
k=num2cell(find(d~=0)+1);
X=double(cellfun(@(z)vpasolve(Y==0,r,x(z)),k));
Y1 = unique(X*exp(1j*pi/3));
end
in matlab code works perfectly, but when i compile it in standaolne app i have the next error:
"syms, subs, vpasolve" are excluded from packaging for the MATLAB Runtime environment according to the MATLAB Compiler license. Either remove the file or function from your code, or use the MATLAB function "isdeployed" to ensure the function is not invoked in the deployed component.
What I did: using matlabFunction, I obtained an expression for y, then substituting q into it, I obtained them numerically. After that I could find points on the X axis where y > 0. But then how to substitute all this into vpasolve and get an expression for it - I don't understand....
code for y expression:
syms r q
Y=airy(1,-r)-q*airy(-r);
x=0:0.1:20;
y=subs(Y,r,x);
f = matlabFunction(y)
but how to get the same for all 'roots_2021a' function?

Accepted Answer

Dyuman Joshi
Dyuman Joshi on 12 Oct 2023
Convert the function to use numeric functions and operations -
%Random input
in=rand;
%Output corresponding to the input
Out1 = modified(in);
Out2 = roots_2021a(in);
As we are dealing with floating point numbers, thus using a tolerance to compare
%Comparison
all(abs(real(Out1)-real(Out2))<1e-10)
ans = logical
1
all(abs(imag(Out1)-imag(Out2))<1e-10)
ans = logical
1
norm(Out1-Out2)
ans = 1.3154e-14
function Y1 = modified(q)
Y = @(r) airy(1,-r)-q*airy(-r);
x = 0:0.1:20;
y = Y(x);
d = diff(y>0);
k = find(d~=0)+1;
X = arrayfun(@(in) fzero(@(r) Y(r), in), x(k));
Y1 = unique(X*exp(1j*pi/3));
end
function [Y1] = roots_2021a(q)
syms r
Y=airy(1,-r)-q*airy(-r);
x=0:0.1:20;
y=double(subs(Y,r,x));
d=diff(y>0);
k=num2cell(find(d~=0)+1);
X=double(cellfun(@(z)vpasolve(Y==0,r,x(z)),k));
Y1 = unique(X*exp(1j*pi/3));
end
  4 Comments
Dmitry
Dmitry on 12 Oct 2023
Edited: Dmitry on 12 Oct 2023
changed fzero() to fsolve() and it's works!

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 12 Oct 2023
Nothing in the symbolic toolbox can be compiled or had code generated for it. Nothing .
You need to use an interactive session to build your formula and use matlabFunction to convert it to a numeric function handle. You can use the 'file' option to write the code as a .m file -- if you do then watch out for the default 'optimize' option being on when 'file' is used, as historically there have been serious bugs in the optimizer, so commonly you want to turn the optimizer off when you use 'file'
Then in the MATLAB code that is to be compiled or had code generated for it, you would call the function that was written to file.
You would not take any result from such a function and use it in vpasolve() -- at least not in the session that is to be compiled. You would instead convert the vpasolve() to fzero() or fsolve() calls for numeric use.

Categories

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

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!