Azzera filtri
Azzera filtri

How to create a syms vector

2 visualizzazioni (ultimi 30 giorni)
Steven
Steven il 13 Mag 2020
Commentato: Steven il 13 Mag 2020
How do I generate symbolic variables that I can put into a fminsearch function?
x = sym('x', [1 2]);
fun1 = 4*x(1)^2+3*x(2)+2
myfun = matlabFunction(fun1,'Vars',x);
[xout,fval,exitflag,output] = fminsearch(myfun,[1 ,2])
I tried the code above, however myfun ends up being a function of two variables x1 and x2
@(x1,x2) x2.*3.0 + x1.^2.*4.0 + 2.0
So I have to rewrite the function by hand because I don't know how to generate a symbolic function that looks like this
x(1) x(2)
So I end up rewriting the function
@(x) x(2).*3.0+x(1).^2.*4.0+2.0
and this works
myfun2 =@(x) x(2).*3.0+x(1).^2.*4.0+2.0
[xout,fval,exitflag,output] = fminsearch(myfun2,[1 ,2])
So How do I generate a symbolic vector of x that can allow me to turn fun1 into myfun2

Risposta accettata

Walter Roberson
Walter Roberson il 13 Mag 2020
x = sym('x', [1 2]);
fun1 = 4*x(1)^2+3*x(2)+2
myfun = matlabFunction(fun1,'Vars',{x}); %small difference from what you had
[xout,fval,exitflag,output] = fminsearch(myfun,[1 ,2])
  1 Commento
Steven
Steven il 13 Mag 2020
Amazing what curly braces will do, Thanks

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Symbolic Math Toolbox in Help Center e File Exchange

Tag

Prodotti


Release

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by