Defining a mathematical function as the summation of multiple functions

3 visualizzazioni (ultimi 30 giorni)
Hi,
I want to define the function below in MATLAB and then minimized it using fminsearch.
What I did was to define a loop along with syms function to generate this function and then used matlabFunction() command to convert it to a function.
--------------------------------------------------------------------------------------------------------------------
syms x0 y0 z0 b
S=0;
NumberOfNeighbors=100;
for i = 1:NumberOfNeighbors
Xd=xdata(i,1);
Yd=ydata(i,1);
Zd=zdata(i,1);
Sum=abs(((Xd-x0)^2)+((Yd--y0)^2)+((Zd--z0)^2)-b^2);
S=S+Sum;
end
ht = matlabFunction(S)
Init=[0,0,0,0];
Fmin = fminsearch(ht,Init)
--------------------------------------------------------------------------------------------------------------------
My code worked perfectly, but when I use the generated function as an input in fminsearch, I get the following error message:
"Not enough input arguments."
The reason is that the generated function by the matlabFunction() command is a function of (x0 y0 z0 b), while it should be a function of u(1),u(2),u(3) and u(4).
We are not allowed to define variables of the form u(1),u(2),u(3) and u(4) using syms.
I would appreciate any help that you can provide. Thanks.

Risposta accettata

Star Strider
Star Strider il 31 Mar 2022
To get all the arguments as elements of a single vector. use the 'Vars' name-value pair and enclose the arguments in square brackets []:
ht = matlabFunction(S, 'Vars',{[x0,y0,z0,b]})
To see how that works in practice —
xdata = randn(100,1);
ydata = randn(100,1);
zdata = randn(100,1);
syms x0 y0 z0 b
S=0;
NumberOfNeighbors=100;
for i = 1:NumberOfNeighbors
Xd=xdata(i,1);
Yd=ydata(i,1);
Zd=zdata(i,1);
Sum=abs(((Xd-x0)^2)+((Yd--y0)^2)+((Zd--z0)^2)-b^2);
S=S+Sum;
end
ht = matlabFunction(S, 'Vars',{[x0,y0,z0,b]})
ht = function_handle with value:
@(in1)abs((in1(:,1)+6.031542382955758e-1).^2-in1(:,4).^2+(in1(:,3)-4.514357154410683e-1).^2+(in1(:,2)-1.113945828129602).^2)+abs((in1(:,3)-1.45783769930615e-1).^2+(in1(:,1)+1.087893584703542).^2+(in1(:,2)-1.573349894165783).^2-in1(:,4).^2)+abs((in1(:,1)+3.73489278607807e-2).^2+(in1(:,2)+8.421479945678007e-2).^2-in1(:,4).^2+(in1(:,3)-9.53913989327878e-1).^2)+abs((in1(:,2)+1.739400576709651).^2+(in1(:,1)-1.254083115563258).^2-in1(:,4).^2+(in1(:,3)+3.640522000383975e-1).^2)+abs((in1(:,1)+1.420768366051117).^2+(in1(:,2)-2.369807124107065e-1).^2+(in1(:,3)-4.059170202704624e-1).^2-in1(:,4).^2)+abs((in1(:,2)-4.418970545030066e-1).^2-in1(:,4).^2+(in1(:,1)-9.294218879765488e-1).^2+(in1(:,3)-3.909414337345544e-1).^2)+abs((in1(:,2)+5.893359922897708e-1).^2-in1(:,4).^2+(in1(:,1)+4.017260527656553e-1).^2+(in1(:,3)+5.383304350975927e-1).^2)+abs((in1(:,1)-1.005831521757571).^2+(in1(:,3)-2.48584028231891e-1).^2+(in1(:,2)+2.22135632952146).^2-in1(:,4).^2)+abs((in1(:,1)-2.614894839493323).^2+(in1(:,3)+1.68014627666172).^2+(in1(:,2)-8.753995052294088e-1).^2-in1(:,4).^2)+abs((in1(:,2)+4.250057231383061e-1).^2-in1(:,4).^2+(in1(:,3)+5.958307109475643e-1).^2+(in1(:,1)-1.963442762847612).^2)+abs((in1(:,1)+1.361188709078525).^2+(in1(:,2)+1.788579253947234).^2+(in1(:,3)+1.55732668135481).^2-in1(:,4).^2)+abs((in1(:,3)+1.682161641512796).^2-in1(:,4).^2+(in1(:,1)+1.50312354803044).^2+(in1(:,2)-6.137691803445755e-2).^2)+abs((in1(:,2)+1.777935747636345).^2+(in1(:,1)+2.659359288794637e-1).^2-in1(:,4).^2+(in1(:,3)+1.825161793758771e-1).^2)+abs((in1(:,3)+1.383952830502305).^2+(in1(:,1)-1.26483344600822).^2-in1(:,4).^2+(in1(:,2)+6.273348896641698e-1).^2)+abs((in1(:,2)+1.095837479066349).^2+(in1(:,3)+6.077104329760737e-1).^2+(in1(:,1)+9.064717472692025e-1).^2-in1(:,4).^2)+abs((in1(:,2)+1.31879035499212).^2+(in1(:,1)+1.929669660101218e-1).^2+(in1(:,3)+3.732958753983768e-1).^2-in1(:,4).^2)+abs((in1(:,1)-1.200864405758769).^2+(in1(:,2)+6.27206382466213e-2).^2+(in1(:,3)+5.198673963147101e-1).^2-in1(:,4).^2)+abs((in1(:,1)-1.581547044506651).^2-in1(:,4).^2+(in1(:,2)+3.620925494209677e-1).^2+(in1(:,3)-7.044194738397551e-2).^2)+abs((in1(:,3)-6.548227714747618e-1).^2+(in1(:,1)+2.825735466530732e-1).^2+(in1(:,2)+3.259493628823217e-1).^2-in1(:,4).^2)+abs((in1(:,2)+8.921898061332503e-2).^2+(in1(:,1)+3.082275937268097e-1).^2+(in1(:,3)-3.819268521923411e-1).^2-in1(:,4).^2)+abs((in1(:,2)-2.278773605485153e-1).^2+(in1(:,1)-6.792084827277662e-1).^2+(in1(:,3)+1.021268242380771).^2-in1(:,4).^2)+abs((in1(:,3)-3.79371832206307e-1).^2+(in1(:,1)+9.546033119797254e-1).^2-in1(:,4).^2+(in1(:,2)-1.044470930741223).^2)+abs((in1(:,2)-3.278595881747381e-1).^2+(in1(:,1)+1.461312049815205e-1).^2+(in1(:,3)-1.638932662570746).^2-in1(:,4).^2)+abs((in1(:,3)-1.766735366563113).^2-in1(:,4).^2+(in1(:,1)+4.787799542333162e-1).^2+(in1(:,2)-6.395823993438856e-1).^2)+abs((in1(:,3)-1.845733333167634).^2+(in1(:,2)+3.411769508739287e-2).^2+(in1(:,1)-4.783777414294033e-1).^2-in1(:,4).^2)+abs((in1(:,3)+1.457043078662576).^2-in1(:,4).^2+(in1(:,2)+6.483781581359259e-1).^2+(in1(:,1)-1.425926882549134).^2)+abs((in1(:,2)-1.440733474340873e-1).^2+(in1(:,3)-3.207188382667299e-1).^2+(in1(:,1)+8.426540081158101e-1).^2-in1(:,4).^2)+abs((in1(:,3)+3.610479448645704e-1).^2+(in1(:,2)-6.227386698512922e-1).^2+(in1(:,1)+7.528681220823847e-1).^2-in1(:,4).^2)+abs((in1(:,3)+5.479812064743947e-1).^2+(in1(:,1)+1.0483287710631).^2+(in1(:,2)+5.359958852619184e-4).^2-in1(:,4).^2)+abs((in1(:,3)-4.39298391951065e-1).^2+(in1(:,1)+3.629444385673342e-1).^2+(in1(:,2)-4.219839195636151e-1).^2-in1(:,4).^2)+abs((in1(:,1)+1.850306756465896e-1).^2+(in1(:,2)+9.064343571726161e-1).^2-in1(:,4).^2+(in1(:,3)-7.959049051103456e-1).^2)+abs((in1(:,3)+1.19869242299813).^2+(in1(:,2)-8.429389697296127e-1).^2-in1(:,4).^2+(in1(:,1)+2.898944054138064).^2)+abs((in1(:,3)-1.036116495883203).^2+(in1(:,2)+1.26954868098272).^2-in1(:,4).^2+(in1(:,1)-7.880260812273465e-1).^2)+abs((in1(:,2)+1.749780575106909).^2+(in1(:,3)+5.15561660582576e-1).^2-in1(:,4).^2+(in1(:,1)+1.108622159833217e-1).^2)+abs((in1(:,1)+1.279103201379184).^2+(in1(:,2)-3.127398926250695e-1).^2-in1(:,4).^2+(in1(:,3)-7.745531578740871e-1).^2)+abs((in1(:,2)-1.555708032993002).^2+(in1(:,1)-5.693922664792982e-1).^2-in1(:,4).^2+(in1(:,3)+4.871536081882957e-1).^2)+abs((in1(:,2)+2.584045753466138e-1).^2+(in1(:,3)-9.35470686819427e-1).^2-in1(:,4).^2+(in1(:,1)-2.415589178446184e-1).^2)+abs((in1(:,3)-8.007172628545135e-1).^2+(in1(:,1)+3.811013483931354e-1).^2+(in1(:,2)-3.185379809688022e-1).^2-in1(:,4).^2)+abs((in1(:,3)-3.357927323835146e-1).^2+(in1(:,2)+1.130949547274701).^2+(in1(:,1)+6.8617747680658e-1).^2-in1(:,4).^2)+abs((in1(:,3)-7.856936284014074e-1).^2+(in1(:,1)+2.260270318231605e-1).^2-in1(:,4).^2+(in1(:,2)+8.79969513358513e-1).^2)+abs((in1(:,2)-1.309495598598901).^2+(in1(:,1)-7.486826232772817e-1).^2-in1(:,4).^2+(in1(:,3)-3.387412490796151e-1).^2)+abs((in1(:,1)+1.021339881713544).^2-in1(:,4).^2+(in1(:,2)-1.128049502818774).^2+(in1(:,3)-6.388404335532554e-1).^2)+abs((in1(:,3)-7.992923248057919e-2).^2+(in1(:,2)+4.223176716253297e-1).^2-in1(:,4).^2+(in1(:,1)+1.723942982038855e-2).^2)+abs((in1(:,3)+8.805483687555971e-1).^2+(in1(:,1)-8.71219464542484e-1).^2+(in1(:,2)+1.600664982533865e-1).^2-in1(:,4).^2)+abs((in1(:,1)+1.037712101215818).^2+(in1(:,2)-1.945341351130151).^2-in1(:,4).^2+(in1(:,3)-5.371121300212048e-1).^2)+abs((in1(:,2)+4.439319977238837e-1).^2+(in1(:,1)+6.626348615874788e-1).^2+(in1(:,3)+8.223428847467215e-1).^2-in1(:,4).^2)+abs((in1(:,2)+2.598557761976337e-1).^2+(in1(:,1)+2.319150104701994).^2-in1(:,4).^2+(in1(:,3)+1.177823576835883).^2)+abs((in1(:,1)-7.358747094093386e-1).^2+(in1(:,3)+1.514512744614416).^2-in1(:,4).^2+(in1(:,2)-4.516173878426457e-1).^2)+abs((in1(:,2)+4.65173196441807e-1).^2+(in1(:,1)+1.702668131208851).^2-in1(:,4).^2+(in1(:,3)+7.155126238963958e-1).^2)+abs((in1(:,3)-1.680520804030173).^2+(in1(:,2)+6.084251446827007e-1).^2+(in1(:,1)-8.708458784881817e-1).^2-in1(:,4).^2)+abs((in1(:,2)+7.628306279874535e-1).^2+(in1(:,3)-2.885155193806651e-1).^2+(in1(:,1)-5.923553011742246e-1).^2-in1(:,4).^2)+abs((in1(:,1)-1.532986156876533).^2+(in1(:,3)-3.853671976051011e-1).^2-in1(:,4).^2+(in1(:,2)-9.63426289787318e-1).^2)+abs((in1(:,3)+5.687075535794989e-1).^2+(in1(:,2)-4.823847368304289e-1).^2-in1(:,4).^2+(in1(:,1)+8.044311850593071e-2).^2)+abs((in1(:,1)+8.770808943817181e-1).^2+(in1(:,3)-7.533199812568616e-1).^2-in1(:,4).^2+(in1(:,2)+3.613704545958411e-1).^2)+abs((in1(:,2)-2.244941533860216).^2+(in1(:,1)+1.767967564317649).^2-in1(:,4).^2+(in1(:,3)-9.2793031881044e-2).^2)+abs((in1(:,1)-1.149650442911343).^2+(in1(:,2)+9.579270532925499e-1).^2-in1(:,4).^2+(in1(:,3)+2.325592882529798e-1).^2)+abs((in1(:,3)-1.770969677447385e-1).^2+(in1(:,2)-1.270317238652013e-1).^2-in1(:,4).^2+(in1(:,1)+6.331456380213899e-1).^2)+abs((in1(:,1)+5.58876793715162e-1).^2+(in1(:,2)+3.709686504074081e-2).^2+(in1(:,3)-4.972010067301731e-1).^2-in1(:,4).^2)+abs((in1(:,1)+3.645526923833446e-1).^2+(in1(:,2)+8.534122332146028e-1).^2+(in1(:,3)-5.482361899787442e-1).^2-in1(:,4).^2)+abs((in1(:,2)+6.621724285316488e-1).^2+(in1(:,3)-6.238193507028985e-3).^2-in1(:,4).^2+(in1(:,1)-1.519968656938935).^2)+abs((in1(:,1)+9.591607629658131e-1).^2+(in1(:,2)+2.135475715219767e-1).^2-in1(:,4).^2+(in1(:,3)+1.146593952937865e-1).^2)+abs((in1(:,1)+1.83675750293157).^2+(in1(:,2)-1.82115442802279e-1).^2-in1(:,4).^2+(in1(:,3)+9.226654550200927e-1).^2)+abs((in1(:,3)+1.525851506516033).^2+(in1(:,1)+5.982338552084356e-1).^2+(in1(:,2)+6.632792603586978e-1).^2-in1(:,4).^2)+abs((in1(:,2)+1.771360892550024e-1).^2+(in1(:,1)+1.308596965900841).^2+(in1(:,3)+1.128677054600566).^2-in1(:,4).^2)+abs((in1(:,3)+1.42036843763625).^2+(in1(:,2)+2.520332990293411e-2).^2-in1(:,4).^2+(in1(:,1)-1.136120600910209).^2)+abs((in1(:,3)-6.812956344291832e-1).^2+(in1(:,2)+3.160177861630217e-1).^2+(in1(:,1)+1.366703047435726).^2-in1(:,4).^2)+abs((in1(:,3)+1.855241393506674).^2+(in1(:,2)+7.879206089472657e-1).^2-in1(:,4).^2+(in1(:,1)+1.384004297301628).^2)+abs((in1(:,2)-1.776072980078977e-1).^2+(in1(:,1)+7.843482505605791e-1).^2-in1(:,4).^2+(in1(:,3)-3.485306719483294e-1).^2)+abs((in1(:,3)-3.352700857089713e-1).^2+(in1(:,2)+1.116476751771697).^2+(in1(:,1)-5.510105169878272e-1).^2-in1(:,4).^2)+abs((in1(:,1)-5.287847679712725e-1).^2+(in1(:,2)+2.580532567027662e-1).^2-in1(:,4).^2+(in1(:,3)-1.610350192255167e-1).^2)+abs((in1(:,3)+1.154593927663631e-1).^2-in1(:,4).^2+(in1(:,2)-1.013269049041128).^2+(in1(:,1)+4.639336150480445e-1).^2)+abs((in1(:,2)+6.760269044830838e-1).^2+(in1(:,3)-1.850134974440107).^2+(in1(:,1)+9.19950677726953e-1).^2-in1(:,4).^2)+abs((in1(:,3)-6.304380202562485e-1).^2+(in1(:,2)-9.328753779139993e-1).^2-in1(:,4).^2+(in1(:,1)+7.793344025172328e-1).^2)+abs((in1(:,1)+2.864349569668414).^2+(in1(:,3)+1.667535175399621).^2-in1(:,4).^2+(in1(:,2)-1.160006411609129).^2)+abs((in1(:,3)-3.798251750810286e-1).^2+(in1(:,1)+2.001982962774807e-1).^2+(in1(:,2)+4.340966487145883e-1).^2-in1(:,4).^2)+abs((in1(:,3)+1.434806139968263e-1).^2+(in1(:,1)+1.73272583344348).^2+(in1(:,2)+1.255941222932033).^2-in1(:,4).^2)+abs((in1(:,2)-1.782366729918602).^2+(in1(:,3)-1.664138773504831).^2+(in1(:,1)+1.466525150899923).^2-in1(:,4).^2)+abs((in1(:,3)-8.152284181144113e-1).^2+(in1(:,2)+4.963633618936993e-2).^2+(in1(:,1)-1.421581931896251).^2-in1(:,4).^2)+abs((in1(:,3)+8.104133395517478e-1).^2+(in1(:,2)-1.75450037805832).^2+(in1(:,1)-3.094955468092052e-1).^2-in1(:,4).^2)+abs((in1(:,2)-1.352916188458926).^2+(in1(:,3)-5.355401677452235e-1).^2+(in1(:,1)+6.289273805930653e-1).^2-in1(:,4).^2)+abs((in1(:,2)-2.379596754466833e-1).^2+(in1(:,3)-2.130801624014998).^2+(in1(:,1)+2.533414455928281e-1).^2-in1(:,4).^2)+abs((in1(:,3)+3.831517527017279e-1).^2+(in1(:,1)-1.392021299195195).^2+(in1(:,2)+3.155566118173782e-1).^2-in1(:,4).^2)+abs((in1(:,2)-8.158849580237879e-2).^2+(in1(:,3)+7.095785713401557e-1).^2-in1(:,4).^2+(in1(:,1)+1.656200582510875e-1).^2)+abs((in1(:,2)-6.506295920496573e-1).^2+(in1(:,3)-1.378850931981877).^2-in1(:,4).^2+(in1(:,1)-2.913371163942933e-1).^2)+abs((in1(:,3)+5.43511812131464e-1).^2+(in1(:,2)-1.193650199361929).^2+(in1(:,1)+9.326013358897691e-1).^2-in1(:,4).^2)+abs((in1(:,3)-1.25376913901259).^2+(in1(:,2)+1.17012674076671).^2-in1(:,4).^2+(in1(:,1)-5.313065504354205e-1).^2)+abs((in1(:,3)+5.254125343078276e-1).^2+(in1(:,1)-5.55158126541898e-1).^2+(in1(:,2)-2.930711078161298e-1).^2-in1(:,4).^2)+abs((in1(:,2)+1.461252251647627).^2+(in1(:,3)+1.108496623161207).^2-in1(:,4).^2+(in1(:,1)+5.003231999415112e-1).^2)+abs((in1(:,3)-2.399970223748402e-1).^2+(in1(:,1)-3.096003525615665e-1).^2-in1(:,4).^2+(in1(:,2)-7.185311441992477e-1).^2)+abs((in1(:,3)+1.314318464910931).^2+(in1(:,1)+1.09737803133872e-1).^2-in1(:,4).^2+(in1(:,2)+4.069016614242763e-1).^2)+abs((in1(:,1)-1.438189873280947e-1).^2+(in1(:,2)-6.59181765090162e-1).^2-in1(:,4).^2+(in1(:,3)+6.679061104676695e-1).^2)+abs((in1(:,3)-3.320129715836648e-1).^2+(in1(:,2)+1.932888431680579e-1).^2-in1(:,4).^2+(in1(:,1)+1.545766956818623).^2)+abs((in1(:,3)+9.858908931056823e-2).^2+(in1(:,1)-1.421038070431656).^2-in1(:,4).^2+(in1(:,2)+4.213054258491518e-1).^2)+abs((in1(:,3)+6.599205674831305e-1).^2+(in1(:,2)+1.043073578687401).^2-in1(:,4).^2+(in1(:,1)-1.593215088321644).^2)+abs((in1(:,3)-5.262473663775586e-1).^2+(in1(:,2)+1.192173494244443).^2-in1(:,4).^2+(in1(:,1)+1.065805107277744).^2)+abs((in1(:,2)-2.741391264684298e-1).^2+(in1(:,1)-6.016302194456916e-1).^2+(in1(:,3)-4.31249978778074e-1).^2-in1(:,4).^2)+abs((in1(:,3)-6.840798421194227e-1).^2+(in1(:,1)+1.097939879451589e-1).^2+(in1(:,2)-4.652125895096236e-1).^2-in1(:,4).^2)+abs((in1(:,3)-1.385055397715085).^2+(in1(:,1)-6.716385958416079e-1).^2+(in1(:,2)+6.33179342396904e-1).^2-in1(:,4).^2)+abs((in1(:,2)-9.457196966330426e-1).^2+(in1(:,3)-2.361969182680211).^2+(in1(:,1)+8.859427712877517e-1).^2-in1(:,4).^2)+abs((in1(:,1)-1.363868744265735).^2-in1(:,4).^2+(in1(:,2)-7.275679857997771e-1).^2+(in1(:,3)-9.901317438821206e-1).^2)
Init=[0,0,0,0];
Fmin = fminsearch(ht,Init)
Fmin = 1×4
-0.0582 0.0601 -0.1977 -1.5811
Here, the row vector of the input arguments is called ‘in1’ not ‘u’. There does not appear to be a way to change the name.
.

Più risposte (1)

Jan
Jan il 31 Mar 2022
Modificato: Jan il 4 Apr 2022
Why do you want to create the function symbolically? It is easy to define it directly:
ht = @(u1, u2, u3, u4) sum((xdata(:, 1) - u1) .^ 2 + ...
(xdata(:, 2) - u2) .^ 2 + ...
(xdata(:, 3) - u3) .^ 2 - u4 ^ 2);
  6 Commenti
Jan
Jan il 4 Apr 2022
Maybe you have to change the function to:
ht = @(u) sum((xdata(:, 1) - u(1)) .^ 2 + ...
(xdata(:, 2) - u(2)) .^ 2 + ...
(xdata(:, 3) - u(3)) .^ 2 - u(4) ^ 2);
with a vector u instead of 4 inputs u1,u2,u3,u4.
Memo Remo
Memo Remo il 7 Apr 2022
Yes. It works now! Thanks for letting me know about this efficient way of defining such function. This is very helpful to speed up the calculations. I appreciate your help.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by