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
Click For Summary

Homework Help Overview

The discussion revolves around implementing a specific matrix in MATLAB that represents a coupled oscillation system of N masses. The original poster seeks to reproduce a matrix based on a given formula and to understand how to generalize this for any number of masses.

Discussion Character

  • Exploratory, Mathematical reasoning, Problem interpretation

Approaches and Questions Raised

  • The original poster attempts to construct the matrix using a nested loop structure, questioning how to shift the formula for matrix generation. Other participants suggest more efficient methods, including starting with a diagonal matrix.
  • Some participants inquire about the meaning of "twos" in the context of matrix construction.
  • There are discussions about function calls and input arguments, with participants seeking clarification on how to properly invoke a function to create matrices of varying sizes.
  • One participant shares a code snippet for generating matrices and expresses confusion about why the expected wave motion is not occurring in their plot.
  • Another participant elaborates on the goal of plotting the position of masses over time, indicating a desire to visualize the dynamics of the system.

Discussion Status

The discussion is active, with participants exploring different coding strategies and clarifying misunderstandings about function usage. There is no explicit consensus on a single approach, but several productive directions are being examined, including matrix generation techniques and plotting methods.

Contextual Notes

Participants are working under the constraints of homework rules, which may limit the amount of direct assistance they can provide. The original poster's goal is to replicate the matrix for both a fixed number of masses and a variable number of masses, which introduces complexity into the discussion.

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 ?
 
I mean twos (2 plural).
 
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
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
3K
Replies
0
Views
2K
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
5
Views
2K
Replies
19
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K