Skip to content
Snippets Groups Projects
Commit adcaee83 authored by JB's avatar JB
Browse files

added nlp problem

parent 89fed2bb
No related branches found
No related tags found
No related merge requests found
clear all; close all; clc;
import casadi.*
......@@ -2,41 +2,40 @@ clear all; close all; clc;
import casadi.*
%% Hanging chain
N = 15;
m = 40/N; % mass [kg]
D = 70*N; % spring constant [J/m^2]
g = 9.81; % gravitational constant [m/s^2]
L = 5/N; % reference length [m]
opti = Opti();
x = opti.variable(N);
y = opti.variable(N);
yr = opti.parameter();
V = 0.5*D*sum((sqrt(diff(x).^2+diff(y).^2)-L).^2,1);
V = V + g*sum(m*y,1);
opti.minimize(V);
opti.subject_to([x(1);y(1)] == [-2;0]);
opti.subject_to([x(N);y(N)] == [2;yr]);
opti.set_initial(x,linspace(-2,2,N));
% Quiet
options = struct;
options.qpsol = 'qrqp';
options.convexify_strategy = 'regularize';
options.print_iteration = false;
options.print_time = false;
options.print_status = false;
options.print_header = false;
options.max_iter = 10000;
options.qpsol_options.print_iter = false;
options.qpsol_options.print_header = false;
opti.solver('sqpmethod',options);
par_nlp = opti.to_function('par_nlp',{yr},{x(round(N/2))},{'yr'},{'y'});
N = 40; % Number of points (chosen by us)
m = 40 / N; % Weight of each section
D = 70 * N; % Stifness coeficient
g = 9.81; % gravitation acceleration
L = 1; % length of the chain
% Set up optimization environment
opti_var = casadi.Opti();
% Problem 1: Simple hanging chain
% Decision variables: positions
x = opti_var.variable(1, N);
y = opti_var.variable(1, N);
Vg = g*sum(m*y);
Vs = 0.5*D*sum((x(1:N-1)-x(2:N)).^2+(y(1:N-1)-y(2:N)).^2);
cost_NLP = Vg + Vs;
opti_var.minimize(cost_NLP);
opti_var.subject_to(x(1) == -2);
opti_var.subject_to(y(1) == 1);
opti_var.subject_to(x(end) == 2);
opti_var.subject_to(y(end) == 1);
opti_var.subject_to(y >= cos(0.1 * x) - 0.5);
opti_var.solver('ipopt');
sol_NLP = opti_var.solve();
% Plotting the results
figure; hold on;
plot(sol_NLP.value(x), sol_NLP.value(y), '-o');
xs = linspace(-2, 2, 1000);
plot(xs, cos(0.1 * xs) - 0.5, '--r');
grid on;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment