필터 지우기
필터 지우기

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

조회 수: 4 (최근 30일)
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

채택된 답변

Sam Chak
Sam Chak 2022년 10월 27일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Performance and Memory에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by