How to get numerical equation for my code?
2 次查看(过去 30 天)
显示 更早的评论
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?
采纳的回答
Dyuman Joshi
2023-10-12
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)
all(abs(imag(Out1)-imag(Out2))<1e-10)
norm(Out1-Out2)
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 个评论
更多回答(1 个)
Walter Roberson
2023-10-12
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Symbolic Math Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!