Filter löschen
Filter löschen

Using fminsearch and then using the output of fminsearch in cost and ode function.

4 Ansichten (letzte 30 Tage)
Using initial value of -100 for fminsearch, After obtaining optimal value of lambda by fminsearch, how to use the output of fminsearch in function?How to remove the error?
%main file
close all;
clc;
clear;
[lambdas,cost] = fminsearch( @(x) Q1_cost, [ -100 ] );
Not enough input arguments.

Error in solution>Q1_cost (line 14)
[interval,sol] = ode45( @(t,x)Q1_ode,[0,10], [0.5;lambdas] );

Error in solution (line 6)
[lambdas,cost] = fminsearch( @(x) Q1_cost, [ -100 ] );

Error in fminsearch (line 201)
fv(:,1) = funfcn(x,varargin{:});
J_min=HW1_Q4_a_J_min_fn(lambdas);
[interval, sol] = ode45( @(t,x) Q1_ode(t,x), [t_0,t_f], [0.5;lambdas] );
% file named with Q1_cost
function [J] = Q1_cost(lambdas)
[interval,sol] = ode45( @(t,x)Q1_ode,[0,10], [0.5;lambdas] );
lambda_t = sol(:,2);
J = lambda_t(end)^2;
end
% file named with Q1_ode
function [xdot] = Q1_ode(t,x)
n = length(x);
xdot = zeros(n,1); % x = [ v(t), gamma(t), h(t) ]
x_1 = x(1);
lambda_0= x(2);
u=log((-1+(x_1/0.8))/(0.015*lambda_0*exp(0.01*t)))
xdot(1) = -0.02*x_1+0.015*u
xdot(2) = (1-exp(u))*2*(x_1/0.8)*(1/0.8)*exp(-0.01*t)-0.015*lambda_0;
end

Akzeptierte Antwort

Sam Chak
Sam Chak am 27 Okt. 2022
Maybe like this:
[lambdas, cost] = fminsearch(@Q1_cost, -100)
lambdas = -0.0411
cost = 264.3093
[interval, sol] = ode45(@Q1_ode, [0 10], [0.5 lambdas]);
plot(interval, sol(:,2))
% Cost
function [J] = Q1_cost(lambdas)
[interval,sol] = ode45(@Q1_ode, [0 10], [0.5 lambdas]);
lambda_t = sol(:,2);
J = lambda_t(end)^2;
end
% ODEs
function xdot = Q1_ode(t,x)
xdot = zeros(2, 1); % x = [ v(t), gamma(t), h(t) ]
x_1 = x(1);
lambda_0 = x(2);
u = log((-1+(x_1/0.8))/(0.015*lambda_0*exp(0.01*t)));
xdot(1) = -0.02*x_1+0.015*u;
xdot(2) = (1-exp(u))*2*(x_1/0.8)*(1/0.8)*exp(-0.01*t)-0.015*lambda_0;
end

Weitere Antworten (0)

Kategorien

Mehr zu Performance and Memory finden Sie in Help Center und File Exchange

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by