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

5 views (last 30 days)
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

Accepted Answer

Sam Chak
Sam Chak on 27 Oct 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

More Answers (0)

Categories

Find more on Performance and Memory in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!