# How to set up fmincon

35 ビュー (過去 30 日間)
sam plant 2018 年 12 月 13 日

I have an objective function which takes input values for a projectile with initial speed v0 and angle theta. Given a target it then calculates the minimum distance of the trajectory to the target and outputs the minimum distance d.
Of course i need to use fmincon to get the minimum distance of the objective function less than 0.1 so that the projectile hits the target but I'm having trouble understanding how to set fmincon up with inputs.
Any Help would be appreciated!

#### 4 件のコメント

sam plant 2018 年 12 月 13 日
It uses ode45
function M=objective(initial,target)
v0=initial(1);
theta=initial(2);
h=0;
dt=0.01;
maxtime=60;
range=solve_ode45(v0,theta,h,dt,maxtime); %calculates projectiles path
compare = [target(1)-range(:,3),target(2)-range(:,4)]; %compares displacement of target to line at all points
d=sqrt(compare(:,1).^2+compare(:,2).^2); %magnitude of displacement to give distance at all points
M=min(d); %finds the minimum distance
madhan ravi 2018 年 12 月 13 日
solve_ode45??
sam plant 2018 年 12 月 13 日
that's another function i had to write using ode45, i have to write 3 functions inside of each other

サインイン to comment.

### 採用された回答

Torsten 2018 年 12 月 13 日

function main
v0=...; % start value for initial velocity
theta0=...; % start value for angle
target=[... , ...]; % target position
x0 = [v0, theta0];
x = fminsearch(@(x)objective(x,target),x0)
end
function M=objective(initial,target)
v0=initial(1);
theta=initial(2);
h=0;
dt=0.01;
maxtime=60;
range=solve_ode45(v0,theta,h,dt,maxtime); %calculates projectiles path
compare = [target(1)-range(:,3),target(2)-range(:,4)]; %compares displacement of target to line at all points
d=sqrt(compare(:,1).^2+compare(:,2).^2); %magnitude of displacement to give distance at all points
M=min(d); %finds the minimum distance
end

#### 1 件のコメント

sam plant 2018 年 12 月 13 日
thank you so much! it's worked!

サインイン to comment.

### その他の回答 (1 件)

Alan Weiss 2018 年 12 月 13 日
Take a look at Optimize an ODE in Parallel. You probably need to specify a time at which the projectile hits the closest to the target. In the example, we use fzero to find when the projectile hits the ground, and you will probably have to define a similar stopping criterion and use fzero also. Or maybe use an interpolation of the trajectory to find the minimum distance. You can easily interpolate an ODE45 solution by using deval.
Alan Weiss
MATLAB mathematical toolbox documentation

#### 0 件のコメント

サインイン to comment.

サインイン してこの質問に回答します。

Translated by