Implement the formula -x(i-1) + 2x(i) -x(I+1) in Matlab

  • Thread starter Thread starter Firben
  • Start date Start date
  • Tags Tags
    Formula Matlab
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
7 replies · 2K views
Firben
Messages
141
Reaction score
0

Homework Statement


I want to reproduce the following matrix:

A=

[1 -1 0 0

-1 2 -1 0

0 -1 2 -1

0 0 -1 1 ]

What i want is to reproduce the second and third row with the formula -x(i+1) + 2x(i) -x(i-1) = 0

This is a coupled oscillation system of N masses (hanging in a rope).
The first row in the matrix is the leftmost mass m hanging from a rope and the last row is the rightmost mass m . The other two rows are the intermediate masses between mass 1 and mass 2. I want to know how i can write the correct code so that i can replicate the matrix both for 4 masses and N massesNote that i is an index and not a multiplication index.

Homework Equations



-x(i+1) + 2x(i) -x(i-1) = 0

The Attempt at a Solution



A = [1 -1 0 0];

for i = 1:1:4
for j = 2:1:4

A(i,4) = -1*(j-1) + 2*j - 1*(j+1)

end
end

How do i shift the formula 1 step to the right ?
 
Physics news on Phys.org
The loop for j should depend on i.

By the way, there are more efficient ways of building that matrix, since you don't really need to do calculations (hint: you can start with a diagonal matrix of 2s).
 
What do you mean with 2s ?
 
If i imply the this code:function [K,T,B,C] = KTBC(n)
% Create the four special matrices assuming n>1

K = toeplitz ([2 -1 zeros(1,n-2)]);
T = K; T(1,1) = 1;
B = K; B(1,1) = 1; B(n,n) = 1;
C = K; C(1,n) = -1; C(n,1) = -1;
end

How can i make a matrix for any value N ? for example N = 20
When i run the code i get the message:
Not enough input arguments.
 
Firben said:
When i run the code i get the message:
Not enough input arguments.

How are you calling the function?
 
I remaked the code to the following:

Y = 4;
for n = 1:1:4

K = toeplitz([3 - 1 zeros(1,n-2)]);
K(1,1) = 1;
K(1,2) = -1;

for N =1:1:2
K(N+1,N) = -1;
K(N+1,N+2) = -1;

end

K(Y,3) = -1;
K(Y,4) = 1;

ends = 15;
m = 0.1;

w = sqrt(s/m);

X = w*K;

[V,D] = eig(X);

wi = sqrt(D);

for t = 0:0.1:1
for j =1:1:4

for l = 0:1:3

x = exp(i*wi(l+1,l+1)*t)*V(:,j) + exp(-i*wi(l+1,l+1)*t)*V(:,j);

hold on
plot(t,x,'b');

end

end
endBut nothing happends. It should be a wave that is moving to the right
 
Last edited:
This code should generate a wave that should move from the left to the right. The code is about couple oscillations. Consider the system of N msses of mass m hanging in strings. I should write a MATLAB script that should solve a system based on given initial values for the positions and velocities for each masses.
How can i plot the position of the masses for a number of time instances of an initial velocity of the first mass ?

Here is my code:
Y = 4; % Four Masses

for n = 1:1:4
K = toeplitz([3 - 1 zeros(1,n-2)]);

K(1,1) = 1;

K(1,2) = -1;
for N =1:1:2

K(N+1,N) = -1;

K(N+1,N+2) = -1;
end
K(Y,3) = -1;

K(Y,4) = 1;
ends = 15; %Spring constant

m = 0.1; %mass 0.1 kg
w = sqrt(s/m); % Angular Frequency
X = w*K; % Angular Frequency times the matrix K
[V,D] = eig(X);

% V is the eigenvectors

wi = sqrt(D); %EigenFrequency

t = 1:1:4;for j =1:1:4
for l = 0:1:3x(t) = exp(i*wi(l+1,l+1)*t(:,4))*V(:,j) + exp(-i*wi(l+1,l+1)*t(:,4))*V(:,j);

% The function that i want to print put
hold on

plot(t,x(t),'b');
end
end