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 ] );
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
0 个评论
采纳的回答
Sam Chak
2022-10-27
Maybe like this:
[lambdas, cost] = fminsearch(@Q1_cost, -100)
[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 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!