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

In summary, the conversation is about reproducing a matrix using a formula for a coupled oscillation system of N masses. The conversation also discusses the use of efficient methods to build the matrix and how to make the matrix for any value of N. The conversation then moves on to plotting the position of the masses for a given initial velocity.
  • #1
Firben
145
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
  • #2
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).
 
  • #3
What do you mean with 2s ?
 
  • #4
I mean twos (2 plural).
 
  • #5
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.
 
  • #6
Firben said:
When i run the code i get the message:
Not enough input arguments.

How are you calling the function?
 
  • #7
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:
  • #8
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
 

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

What does the formula -x(i-1) + 2x(i) -x(i+1) represent?

The formula represents a mathematical operation that involves the values of three consecutive elements in a given array, where x(i-1) represents the value of the element before x(i), x(i) represents the value of the current element, and x(i+1) represents the value of the element after x(i).

How do I implement this formula in Matlab?

To implement this formula in Matlab, you can use a for loop to iterate through the array and perform the necessary calculations for each element. You can also use the built-in functions for indexing and arithmetic operations to simplify the code.

Can this formula be used on any type of data in Matlab?

Yes, this formula can be used on any type of data in Matlab, as long as the data is stored in an array or matrix format and can be accessed using indexing.

Are there any limitations to using this formula in Matlab?

One potential limitation of using this formula in Matlab is that it can only be applied to one-dimensional arrays or matrices. It may also not be suitable for very large arrays, as it can be computationally intensive to perform the calculations for each element.

Can this formula be modified for different mathematical operations?

Yes, this formula can be modified to perform different mathematical operations by changing the coefficients and operators used. However, it is important to ensure that the modified formula is mathematically valid and applicable to the data being used.

Back
Top