MATLAB Answers


How to interpolate a matrix

Asked by Serena Solanki on 19 Feb 2018
Latest activity Commented on by Serena Solanki on 20 Feb 2018
Essentially I didn't know how to ask this as it probably is simple but I don't quite know what to do.
Essentially I want to create a response function for an earthquake record, where ultimately I want to plot the peak displacement against a varying time period. I am using the Runge-Kutta Method to integrate to get displacement from the acceleration data.
I have got a range of values for the time period -T0 and have calculated the state-space matrix A from this.
essentially I am unsure if i need to interpolate my A matrix to be the same size as my Npts in order for the loop to work.
Hope it makes sense and hope you can help!
Thanks in advance!!
for i=0.1:0.1:4; % Range of Time Periods defined
T0=(i);% seconds
w = (2*pi)/T0(1:end); %rad/s
m=773; % Mass in MG
A1=[0 1
-w2(1:end) -(2*zita*w(1:end))]; %state-space matrix of coefficients
1/m]; %state-space load vector
ft = m*acc.'; %acceleration data- converting to force f=ma
dt = 0.02; %seconds
NPTS = size(ft); %no.of points
tf = (NPTS-1)*dt;
t = 0:dt:tf;
dt_interp = 0.0001;
t_interp = dt_interp:dt_interp:tf;
ft_interp = F_interp(t, ft, t_interp);
NPTS_interp = size(t_interp,2);
yt = zeros(2,t_interp);
for i=1:(NPTS_interp-1)% Runge-Kutta loop, i = incremental time step
g1 = A(1:end)*yt(:,i)+b*ft_interp(i);
g2 = A(1:end)*(yt(:,i)+ 0.5*g1*dt_interp)+0.5*b*(ft_interp(i)+ft_interp(i+1));
g3 = A(1:end)*(yt(:,i)+ 0.5*g2*dt_interp)+0.5*b*(ft_interp(i)+ft_interp(i+1));
g4 = A(1:end)*(yt(:,i)+g3*dt_interp)+b*ft_interp(i+1);
ynew = yt(:,i)+(g1+2*g2+2*g3+g4)*dt_interp/6;
yt(:,i+1)=ynew; %storage
ut=yt(1,:); %displacement values
vt=yt(2,:);%velocity values


Bob Nbob
on 19 Feb 2018
Do you know about the interp1() and interp2() commands? They are built in Matlab commands that I have used for interpolation purposes. These are both linear interpolators, so if you need something more complex you're probably going to need to get help from somebody other than me.
"... interp1() and interp2() commands? .... These are both linear interpolators..."
Reading the interp1 and interp2 documentation shows that they support a lot more than just linear interpolation, including variations of splines and cubic interpolation. The documentation is available online for everyone to read.
"essentially I am unsure if i need to interpolate my A matrix to be the same size as my Npts in order for the loop to work."
Writing code by guessing is not very efficient. Even before you get to writing code down you should first understand the algorithm that you want to implement. Even write it out on paper, and work through a few iterations by hand: this will give you something to compare your code's output against (you do confirm that your code is giving reasonable output values, don't you?) and it allows you to understand the prerequisites and conditions for the algorithm to work.
"How to interpolate a matrix"
Read the iterp1 and interp2 documentation carefully. Try the examples. Try it with your data.
Hi Stephen
Thanks for the advice
I have tried doing this and have found a nested loop may be a better way forward rather than interpolating as it isn't what I actually needed.
Writing it out definitely helped!

Sign in to comment.

0 Answers