Mass Spring Damper optimisation using fminsearch

조회 수: 6 (최근 30일)
Leyah A
Leyah A 2020년 5월 6일
답변: Shishir Reddy 2025년 5월 28일
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
Joshua Ramayrat
Joshua Ramayrat 2020년 11월 8일
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
Joshua Ramayrat
Joshua Ramayrat 2020년 11월 8일
Typically, we usually include a constraint function that depicts the range of the parameters to be optimized.

댓글을 달려면 로그인하십시오.

답변 (1개)

Shishir Reddy
Shishir Reddy 2025년 5월 28일
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.

카테고리

Help CenterFile Exchange에서 Optimization Toolbox에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by