# Using fminsearch() for MSD system

2 views (last 30 days)
Gurkaran Singh on 26 Sep 2022
Edited: Gurkaran Singh on 26 Sep 2022
Hi guys, i am trying to optimize the mass damper system by choosing the optimal values of k2 and c2. I am getting so many errors while using fminsearch(). I am sharing the code below which is providing output for values of x1 and x2 (displacements of 2 masses). Our goal is to minimize x1 after 300s. It would be great if someone can help me modifying this code ofr fminsearch please.
In the code, values of k2 and c2 are selected manually but we need to optimize them using fminsearch()
clc
clear all
close all
tspan = [0:0.1:400]; %observing system's behaviour from 0-400 seconds
x0 = [0;0;0;0]; % as m1 and m2 are at rest at t=0.
%x1(0),x2(0),vel_m1(0) and vel_m2(0)
[t,x] = ode45(@odefun, tspan, x0); %odefun is the name of the function
plot(t,x(:,1)) %first degree
hold on
plot(t,x(:,2)) %second degree
legend('x1','x2')
xlabel('time')
ylabel('displacement')
function dxdt= odefun(t,x) %creating derivative functions
m1=50000; % given in the question
m2=1000; % 2 percent mass of m1
k1=500; % given in the question.
k2=50; % manually selected value to minimize x1
c2=400; % manually selected value to minimize x1
if t<=0.1 %Fd occurs at m1 only for first 100 ms
Fd=1000;
else
Fd=0;
end
%initializing column vector dxdt function
dxdt=zeros(4,1);
dxdt(1)=x(3); %equation 1
dxdt(2)=x(4); %equation 2
dxdt(3)=1/m1*(-(k1+k2)*x(1)+k2*x(2)-c2*(x(4)-x(3))+Fd); %equation 3
dxdt(4)=1/m2*(k2*x(1)-k2*(x(2))+c2*(x(3))-c2*(x(4))); %equation 4
end
##### 2 CommentsShowHide 1 older comment
Gurkaran Singh on 26 Sep 2022
Edited: Gurkaran Singh on 26 Sep 2022
Thanks Alan for commenting. Our object is to minimize x1 after 300 s. Is it possibile to use fminsearch to calculate the values of k2 and c2?

Torsten on 26 Sep 2022
Edited: Torsten on 26 Sep 2022
p0 = [50 400];
p = fminsearch(@fun,p0)
Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: -Inf
p = 1×2
1.0e+06 * 0.2536 -2.4047
function objective = fun(p)
k2 = p(1);
c2 = p(2);
%tspan = [0,0.3]; %observing system's behaviour from 0-400 seconds
x0 = [0;0;0;0]; % as m1 and m2 are at rest at t=0.
%x1(0),x2(0),vel_m1(0) and vel_m2(0)
tspan = [0,0.1];
iflag = 1;
[t,x] = ode45(@(t,x)odefun(t,x,k2,c2,iflag), tspan, x0); %odefun is the name of the function
tspan = [0.1,0.3];
x0 = x(end,:);
iflag = 2;
[t,x] = ode45(@(t,x)odefun(t,x,k2,c2,iflag), tspan, x0); %odefun is the name of the function
objective = x(end,1);
end
function dxdt= odefun(t,x,k2,c2,iflag) %creating derivative functions
m1=50000; % given in the question
m2=1000; % 2 percent mass of m1
k1=500; % given in the question.
%k2=50; % manually selected value to minimize x1
%c2=400; % manually selected value to minimize x1
if iflag==1
Fd = 1000;
elseif iflag==2
Fd = 0.0;
end
%if t<=0.1 %Fd occurs at m1 only for first 100 ms
% Fd=1000;
%else
% Fd=0;
%end
%initializing column vector dxdt function
dxdt=zeros(4,1);
dxdt(1)=x(3); %equation 1
dxdt(2)=x(4); %equation 2
dxdt(3)=1/m1*(-(k1+k2)*x(1)+k2*x(2)-c2*(x(4)-x(3))+Fd); %equation 3
dxdt(4)=1/m2*(k2*x(1)-k2*(x(2))+c2*(x(3))-c2*(x(4))); %equation 4
end

### Categories

Find more on Problem-Based Optimization Setup in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by