How to create a matrix with variables?

In summary: For example:A = [1 0 2; 3 4 5; 0 2 3];X = [1 2 3; 4 5 6];A(:,1) = X(:,1);A(:,2) = X(:,2);A(:,3) = X(:,3);A(:,4) = X(:,4);This would create the matrix as shown in the image.
  • #1
influx
164
2
Hello,

I am kind of new to Matlab so the questions I will ask probably sound a bit basic. Anyways, here goes:

I want to create the matrix below which has both constants and variables. How can I do this? I know how to create a normal matrix (e.g. B = [1 0 2; 3 4 5; 0 2 3]) but I don't know how to create matrices with variables in them?

matrix.PNG


I tried to type the matrix in as is shown above but it doesn't seem to work. I'm pretty sure I should be following some sort of method but I have no clue.

Apart from element a31, all the elements are constants that I've been given (Fc, Vc, Vr, Fr and dt are constants). Element a31 can vary since the ''Act'' term can vary. I understand that I can just enter the constants instead for the other elements, but for now I'd like to enter the elements so it looks like the image I posted.

EDIT: I'd like to add that in element a44, there should be no ''/'' sign. That is a mistake. It should be just 1-Fc*dt/Vc.

Thanks!
 
Physics news on Phys.org
  • #2
I'm not entirely sure that you can use symbolic variables in a MATLAB matrix like that. If the variable act changes value over time, I'd create a loop with the number of time steps desired and calculate the value of act during each iteration, and then in that same iteration manually update the matrix with the new value.

e.g.

Code:
    act = ...;
    %Value of act at t=0.

    %...Now initialize the matrix...%

    matrix(3,1)=act*fr*dt/vr;
    %Substitute the correct value into the (3,1) slot of the matrix%

    for j=1:1:numberOfTimeSteps

        act=...;
        %Value of act at time step j.
        matrix(3,1)=act*fr*dt/vr;

         %...matrix calculations...%

    end
 
  • #3
johnnyTransform said:
I'm not entirely sure that you can use symbolic variables in a MATLAB matrix like that. If the variable act changes value over time, I'd create a loop with the number of time steps desired and calculate the value of act during each iteration, and then in that same iteration manually update the matrix with the new value.

e.g.

Code:
    act = ...;
    %Value of act at t=0.

    %...Now initialize the matrix...%

    matrix(3,1)=act*fr*dt/vr;
    %Substitute the correct value into the (3,1) slot of the matrix%

    for j=1:1:numberOfTimeSteps

        act=...;
        %Value of act at time step j.
        matrix(3,1)=act*fr*dt/vr;

         %...matrix calculations...%

    end

Act doesn't vary over time.

To clarify, the problem is as follows:

X(t) = AX(t-1)

Where X(t) is a column vector consisting of elements X1(t), X2(t), X3(t) and X4(t). Matrix A is the matrix in the image above and X(t-1) is a column vector consisting of elements X1(t-1), X2(t-1), X3(t-1) and X4(t-1).

So the multiplication of matrix A (in the image above) with X(t-1) yields us X(t) [which is the value that comes after X(t-1)]. Essentially, matrix A is used to help yield a prediction of the next state [X(t)] and this will then be compared to the actual next state obtained via measurement.

The value of Act (which represents activity) depends on the subject tested. For each subject (i.e individual) tested, I have an Excel file with the results of their tests and the value for Act for each subject is within this excel file.

So for each subject, I need to multiply matrix A with column vector X(t-1) but as I explained, each subject will have had a different Act.

I want to write the matrix A exactly as in the picture, then have the ability to assign values to Act and the other elements (the other elements will be assigned constant values for all subjects but Act will vary depending on the subject). I am not sure if it is possible to create a loop to (by selecting the excel files?) or whether I have to manually assign the value of Act each time?

I hope that clarifies it!
 
Last edited:
  • #4

1. How do I define variables in a matrix?

To define variables in a matrix, you can use the "syms" function in MATLAB or "var" function in R. This will allow you to create symbolic variables that can be used in your matrix.

2. Can I use both numerical values and variables in a matrix?

Yes, you can use a combination of numerical values and variables in a matrix. This can be useful for creating dynamic matrices that can be easily modified by changing the values of the variables.

3. How do I assign values to variables in a matrix?

To assign values to variables in a matrix, you can use the "subs" function in MATLAB or the "substitute" function in R. This will allow you to substitute specific values for your variables in the matrix.

4. Can I perform operations on variables in a matrix?

Yes, you can perform operations on variables in a matrix, just like you would with numerical values. This can be done using arithmetic operators such as addition, subtraction, multiplication, and division.

5. How can I display the contents of a matrix with variables?

To display the contents of a matrix with variables, you can use the "disp" function in MATLAB or the "print" function in R. This will print the matrix with the substituted values for the variables.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
32
Views
2K
  • Linear and Abstract Algebra
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Differential Equations
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • Calculus and Beyond Homework Help
Replies
3
Views
325
  • Engineering and Comp Sci Homework Help
Replies
22
Views
1K
Back
Top