Minimalizing a function of two variables

1 visualización (últimos 30 días)
Charles Mitchell-Thurston
Charles Mitchell-Thurston el 6 de Jun. de 2022
Comentada: Charles Mitchell-Thurston el 6 de Jun. de 2022
Hopefully my final question for a while.
I have my final function
FullScore(P1,P2)
This takes in experimental data, and works out the mean difference between this and data that is produced using P1 and P2. Origionally i was going to try use lsqcurvefit/lsqnonlin but because of my previous question my experimental data and simulated data are both used to produce the Y values on their respecive graphs.
My goal is to find the values of P1 and P2 that produce data that is the most similar to my experimental data
i am now simply trying
fminsearch(FullScore,[15 0.5]) %P1 goes from 0-35 and P2 0.01-1
When i run the function on its own it works fine, but when i run it from above it doesnt read 15 as P1 and 0.5 as P2 as the fire time these come up in the code i get(this is basically the first line of the code)
Not enough input arguments.
Error in FullScore (line 15)
newtext = [text1, num2str(P1), 'p ', num2str(P2), text2] ;
How can i make it so that my function takes in my two startin guesses correctly?

Respuesta aceptada

Voss
Voss el 6 de Jun. de 2022
Editada: Voss el 6 de Jun. de 2022
First thing. This:
fminsearch(FullScore,[15 0.5])
calls the function FullScore with no inputs; that's why you get that error. You need to send the function handle @FullScore instead:
fminsearch(@FullScore,[15 0.5])
Then you'll get a different error, due to the fact that fminsearch takes functions of one input only. You can get around that by either (1) redefining your function FullScore to take a single input:
function out = FullScore(P1P2)
P1 = P1P2(1);
P2 = P1P2(2);
% ... your code
end
Or (2) make an anonymous function that takes a single 1-by-2 input and sends two scalar inputs to FullScore, and call fminsearch on that anonymous function:
f = @(pp)FullScore(pp(1),pp(2));
fminsearch(f,[15 0.5])
(or, same as above, but without storing the anonymous function as the variable f:)
fminsearch(@(pp)FullScore(pp(1),pp(2)),[15 0.5])
With the second approach you don't have to change the definition of FullScore.
  1 comentario
Charles Mitchell-Thurston
Charles Mitchell-Thurston el 6 de Jun. de 2022
Thanks, you answer made it run.
Sadly the output is wrong but thats a problem for tomorrow. (by wrong i mean it gives P1 and P2 suggestions but the suggestions are bad)

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by