Minimalizing a function of two variables
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Charles Mitchell-Thurston
le 6 Juin 2022
Commenté : Charles Mitchell-Thurston
le 6 Juin 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?
0 commentaires
Réponse acceptée
Voss
le 6 Juin 2022
Modifié(e) : Voss
le 6 Juin 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.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Vehicle Scenarios dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!