1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Nov 15, 2016 #1
    1. The problem statement, all variables and given/known data
    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 masses


    Note that i is an index and not a multiplication index.

    2. Relevant equations

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

    3. 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 ?
     
  2. jcsd
  3. Nov 15, 2016 #2

    DrClaude

    User Avatar

    Staff: Mentor

    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).
     
  4. Nov 15, 2016 #3
    What do you mean with 2s ?
     
  5. Nov 15, 2016 #4

    DrClaude

    User Avatar

    Staff: Mentor

    I mean twos (2 plural).
     
  6. Nov 16, 2016 #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.
     
  7. Nov 16, 2016 #6

    DrClaude

    User Avatar

    Staff: Mentor

    How are you calling the function?
     
  8. Nov 16, 2016 #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;

    end


    s = 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
    end


    But nothing happends. It should be a wave that is moving to the right
     
    Last edited: Nov 16, 2016
  9. Nov 16, 2016 #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;



    end





    s = 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:3





    x(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
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted