Renaming the variabel while Symbolic to function handle conversion with matlabFunction

6 vues (au cours des 30 derniers jours)
I do have the symbolic equation which is given as
A5, A1,(xdata and ydata) are the only variables and rest of them are constants.
Objective: To convert symbolic into function handle with replacing scalar variable (A1, A5) as a Vector A (A(1), A(2)).
When I use matlabFunction(F1),
F1d=vpa(subs(F1,[k m_canti B L Gap rho mu omega],[k_val m_canti_val B_val L_val Gap_val GWM_density(1) xdata ydata]),6)
func = matlabFunction(F1)
I am getting the result as below
func =
@(A1,A5,xdata,ydata)ydata.*-1.0-1.0./sqrt(A5.*3.987559999999988e-1+A1.*sqrt((xdata.*9.968899999999994e+2)./ydata).*2.26274169979699e-4+1.073920000000006e-3).*4.260164316079795
Since I am going to use this function for curve fitting I want A1, A5 to be renamed as A(1) and A(2). Basically I want to introduce a vector A in the above function handle.
I have gone through the documentation of matlabFunction(), Still I couldn't able to understand how to do what I want to achieve. I would be so grateful, if this problem got solved.
Thank you.

Réponse acceptée

Star Strider
Star Strider le 8 Juin 2022
To get the ‘A’ values as a vector, put them in square brackets [].
func = matlabFunction(F1d, 'Vars',{[A1,A5],xdata,ydata})
The result should be:
func = @(in1,xdata,ydata) REST_OF_THE_FUNCTION_CODE
The ‘in1’ argument is a row vector where:
in1(1,:) = A1
in1(2,:) = A5
.
  2 commentaires
Saravanakumar Dharmaraj
Saravanakumar Dharmaraj le 9 Juin 2022
Can I substitute xdata and ydata in the func and convert into function of one variable? like
func=@(in1) REST_OF_THE_FUCTION_CODE
Star Strider
Star Strider le 9 Juin 2022
You could, however that might make things more difficult.
I would just leave the arguments the way they are.
So for example to use it with fminunc, the call would simply be:
x0 = [1, 2]; % Use Appropriate Values For The Initial Parameter Estimates
A = fminunc(@(a)func(a,xdata,ydata), x0) % Estimate Parameters
although it might be necessary to use norm with it:
A = fminunc(@(a)norm(func(a,xdata,ydata)), x0) % Estimate Parameters
Make appropriate changes to use it with other optimisation functions.

Connectez-vous pour commenter.

Plus de réponses (1)

James Tursa
James Tursa le 8 Juin 2022
You could keep your existing code and create another function handle:
funcv = @(A,xdata,ydata)func(A(1),A(2),xdata,ydata)
  3 commentaires
James Tursa
James Tursa le 8 Juin 2022
Modifié(e) : James Tursa le 8 Juin 2022
Please show the code you are currently using (or a small example that exibits this behavior) for creating the function handle. You want some parts of it to be vectors that are currently individual variables? What parts of this building process do you want "automated"? Etc.
Saravanakumar Dharmaraj
Saravanakumar Dharmaraj le 8 Juin 2022
I have updated my question. Please do have a look.
Currently A1, A5 are individual variables. Yes.
Please neglect the world "Automation". I want to find a script to convert individual variables into a vector in function handle.

Connectez-vous pour commenter.

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by