Renaming the variabel while Symbolic to function handle conversion with matlabFunction
7 views (last 30 days)
Show older comments
Saravanakumar Dharmaraj
on 8 Jun 2022
Commented: Star Strider
on 9 Jun 2022
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.
0 Comments
Accepted Answer
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
.
2 Comments
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.
x0 = [1, 2]; % Use Appropriate Values For The Initial Parameter Estimates
A = fminunc(@(a)func(a,xdata,ydata), x0) % Estimate Parameters
A = fminunc(@(a)norm(func(a,xdata,ydata)), x0) % Estimate Parameters
Make appropriate changes to use it with other optimisation functions.
More Answers (1)
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)
3 Comments
James Tursa
on 8 Jun 2022
Edited: James Tursa
on 8 Jun 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.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!