Renaming the variabel while Symbolic to function handle conversion with matlabFunction

5 views (last 30 days)
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 =
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.

Accepted Answer

Star Strider
Star Strider on 8 Jun 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
Star Strider
Star Strider on 9 Jun 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.

Sign in to comment.

More Answers (1)

James Tursa
James Tursa on 8 Jun 2022
You could keep your existing code and create another function handle:
funcv = @(A,xdata,ydata)func(A(1),A(2),xdata,ydata)
Saravanakumar Dharmaraj
Saravanakumar Dharmaraj on 8 Jun 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.

Sign in to comment.




Community Treasure Hunt

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

Start Hunting!