Mass Spring Damper optimisation using fminsearch

I'm trying to optimise the function below. I want to have maximum power output from the Mass Spring Damper by finding the optimal M,C,K and A parameters. I have tried to use fminsearch to find the minumum first with:
x=fminsearch(fhandle,x0,[],M,C,K,A);
but keep getting the errors
  • Index in position 2 exceeds array bounds (must not exceed 1)
  • Error in fminsearch (line 200) fv(:,1) = funfcn(x,varargin{:});
Is there anyway of fixing this or another optimisation approach I could try?
function [power_avg] = MSD(M, C, K, A, transient)
%This fucntion returns power output of MSD system
%Select transient file
%Any transient file can be selected and labelled 'transient'
T = transient(:,1);
dt = 0.01;
head = transient(:,2);
%cutting data
numrows = size(T,1);
cut = round (0.01*numrows);
time = (T(1+cut:numrows-cut))*24*60*60;
H = head(1+cut:numrows-cut);
%Processed Data
P = (5.07*H)+3.53 ;
figure; plot(time,P)
xlabel('Time (s)')
ylabel('Pressure (Bar)')
title ('Processed Data')
%Finite Difference
Pressure = P*100 %Convert to kPa
A = 300*1e-6;
F = Pressure*A;
x(1) = 0 ;
x(2) = 0 ;
B1 = (M/dt^2)+(C/dt)+K;
B2 = (-2*M/dt^2)-(C/dt);
B3 = (M/dt^2);
for i = 3:length(time)
x(i)= (1/B1)*(F(i)-B2*x(i-1)-B3*x(i-2));
v(i)= (x(i)-x(i-1))/dt;
a(i)= (x(i)-2*x(i-1)+x(i-2))/dt^2;
end
%Energy Extraction
E = v.^2.*C;
power = E/dt;
power_avg = mean(power);

6 Comments

Can you show the values of parameters in
x=fminsearch(fhandle,x0,[],M,C,K,A);
fhandle = @MSD
x0 = 0.0001 %initial guess
What are the values of M, C, K, A? Which variable are you trying to optimize among "M, C, K, A, and transient".
M, C, K and A can take any value at the moment they are
M=10 %Mass
K=0.1 %Spring-constant
C=0.001 %Damping coefficient
A= 300*1e-6 %Area
and transient is a file of varying pressures I have uploaded using xlsread
Hi Leyah A,
Me and the team are trying to replicate some of this code (optimization of mass spring dampers for other applications) and we're wondering if you could include all the files for this code? (i.e. objective functions, constraint functions, etc.)
Please email us! Thank you so much.
sahilsuresh.shetty@sjsu.edu
kiarash.behzad@sjsu.edu
joshua.ramayrat@sjsu.edu
Typically, we usually include a constraint function that depicts the range of the parameters to be optimized.

Sign in to comment.

Answers (1)

Hi Leyah
As per my understanding, you are trying to optimize the parameters ‘M’, ‘C’, ‘K’, and ‘A’ in your ‘MSD’ function using ‘fminsearch’ as follows –
x=fminsearch(fhandle,x0,[],M,C,K,A);
However, you are getting the error because, ‘fminsearch’ expects the objective function (‘fhandle’) to take a single vector input such as
x = [M, C, K, A])
But your 'MSD' function takes four separate inputs (M, C, K, A) plus transient.
When you call 'fminsearch' with multiple parameters using varargin, MATLAB tries to pass each one separately, but your function is not designed to accept varargin. This mismatch in input format causes MATLAB to throw an indexing error.
To fix this, you need to wrap your ‘MSD’ function inside an anonymous function that takes a single vector ‘x’, then unpacks it into ‘M’, ‘C’, ‘K’, and ‘A’. Kindly refer to the below example which demonstrates this approach.
% Anonymous wrapper function
fhandle = @(x) MSD(x(1), x(2), x(3), x(4), transient);
% Initial guess for [M, C, K, A]
x0 = [1, 1, 1, 1];
x_opt = fminsearch(fhandle, x0);
% Extract optimal values
M_opt = x_opt(1);
C_opt = x_opt(2);
K_opt = x_opt(3);
A_opt = x_opt(
I hope this helps.

Categories

Products

Release

R2019b

Asked:

on 6 May 2020

Answered:

on 28 May 2025

Community Treasure Hunt

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

Start Hunting!