How to create a matrix with variables?

  • Thread starter Thread starter influx
  • Start date Start date
  • Tags Tags
    Matrix Variables
Click For Summary

Discussion Overview

The discussion revolves around creating a matrix in MATLAB that includes both constants and variables, specifically focusing on how to incorporate a variable that changes based on different subjects. Participants explore methods for initializing and updating the matrix with these variables, as well as the implications of using symbolic variables in MATLAB.

Discussion Character

  • Technical explanation
  • Exploratory
  • Homework-related

Main Points Raised

  • One participant expresses uncertainty about how to create a matrix with both constants and a variable in MATLAB, specifically mentioning the need for a variable that can change over time.
  • Another participant suggests using a loop to update the matrix with the variable value at each time step, indicating that the variable should be calculated during each iteration.
  • A later reply clarifies that the variable 'Act' does not vary over time, but rather depends on the subject being tested, with values stored in an Excel file.
  • One participant proposes using anonymous functions in MATLAB as a potential solution for the problem presented.

Areas of Agreement / Disagreement

There is no consensus on the best method to create and update the matrix with variables, as participants present different approaches and clarify aspects of the problem without reaching a definitive solution.

Contextual Notes

Participants discuss the need for a method to handle a variable that changes based on external data (Excel files) while maintaining constant values for other elements in the matrix. The discussion includes considerations about the structure of the matrix and the implications of using symbolic variables.

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 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 3 ·
Replies
3
Views
3K
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K