How to create a matrix with variables?

  • Thread starter Thread starter influx
  • Start date Start date
  • Tags Tags
    Matrix Variables
AI Thread Summary
Creating a matrix in MATLAB that includes both constants and variables is achievable, though it requires a specific approach. Users can define constants directly in the matrix while incorporating variables like "Act" through a loop that updates the matrix for each time step or subject. The discussion emphasizes that while constants can be directly placed in the matrix, the variable "Act" must be updated iteratively based on the subject's data from an Excel file. It’s suggested to use anonymous functions for flexibility in defining the matrix, allowing for dynamic updates as "Act" varies across subjects. The multiplication of the matrix with a column vector is crucial for predicting the next state based on previous measurements. Overall, the key takeaway is the importance of structuring the code to accommodate both fixed and variable elements effectively.
influx
Messages
162
Reaction score
1
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
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
 
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:

Similar threads

Replies
2
Views
2K
Replies
1
Views
2K
Replies
32
Views
4K
Replies
4
Views
3K
Replies
1
Views
3K
Replies
7
Views
3K
Replies
3
Views
1K
Back
Top