The Value assigned to variable 'C' might be unused??

Hi, I am struggling with a piece of code listed below: I am getting the error 'The Value assigned to variable 'C' might be unused' next to the first 'C' equation below Is there any solution. Thank you.
A= [0; 0; 10];
B= [5 -2 0; -3 2 -1; 0 0 1];
C= [I3; I2; I1];
C= linsolve(B,A)

 Akzeptierte Antwort

Guillaume
Guillaume am 20 Apr. 2016

2 Stimmen

You create a C matrix by vertically concatenating three matrices/vectors on your first C equation. On the next line, you create a new C matrix by calling linsolve, effectively discarding the C you created on the previous line.
That's what matlab is telling you: you create a C, never use it, and replace it with something else. The solution to your problem depends on your intent. If you did want to use the first C then use a different variable name for the output of linsolve. If you never intended to use that first C, then don't create it.
As a side note, use better variable names anyway, A, B, C, I1, I2, etc. do not tell me anything about the purpose of the variables. Names like numberofpeople, walltemperature, signalfrequency, etc. do.

3 Kommentare

You can't predeclare how a future variable is going to be split in matlab. You have to do the splitting explicitly after the fact:
C = linsolve(B, A);
I3 = C(:, 1); I2 = C(:, 2); I1 = C(:, 3);
Saying that, the above is poor practice. Aside from the poor naming of the variables (no meaning), numbering variables is poor practice. As a rule, if you start numbering variables you're doing it wrong. It is always better to keep all these variables as one in a matrix or cell array. Thus, in your case, you can simply replace any future instance of I3 by C(:, 1), I2 by C(:, 2), etc.
Also notice that your numbering is not consistent with the column of origin (column 1 corresponds to I3, column 3 corresponds to I1). That's a good potential for future bugs.
Ian Smith
Ian Smith am 21 Apr. 2016
Bearbeitet: Ian Smith am 21 Apr. 2016
I entered the code you suggested and this is what comes up:
Error in untitled4 (line 6)
I3 = C(:, 1); I2 = C(:, 2); I1 = C(:, 3);
As a beginner, I am just coming to understand Matlab. I understand some of your points but I still do not understand why this as a whole is not working. Sorry for the inconvenience.
Guillaume
Guillaume am 21 Apr. 2016
Bearbeitet: Guillaume am 21 Apr. 2016
Sorry, I missed the semicolon instead of comma in your C declaration. you're indexing rows not columns, so it should have been:
I3 = C(1, :); I2 = C(2, :); I1 = C(3, :);
But since C is actually a column vector:
I3 = C(1); I2 = C(2); I1 = C(3);
However, much simpler that these unnecessary assignments:
A = [0; 0; 10];
B = [5 -2 0; -3 2 -1; 0 0 1];
C = linsolve(B,A);
D = C - [0; C(1:2)]; %substract nothing from 1st element, 1st from 2nd, 2nd from third
Note that the comments I've made about meaningful variable names, consistency, avoiding numbered variables, etc. are not particular to matlab. They apply to writing code in any language.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Shu-Han
Shu-Han am 31 Okt. 2023
I keep getting this error message that I don't know how to create the data.
Incorrect use of '=' operator. Assign a value to a variable using '=' and compare values
for equality using '=='.
var C K L w r A;
varexo e;
parameters rho delta gamma alpha lambda g;
alpha = 0.33;
delta = 0.1;
rho = 0.03;
lambda = 0.97;
gamma = 0;
g = 0.015;
model;
1/C= 1/(1+rho)*(1/(C(+1)*(1+g)))*(r(+1)+1-delta);
L^gamma = w/C;
r = alpha*A*(K(-1)/(1+g))^(alpha-1)*L^(1-alpha);
w = (1-alpha)*A*(K(-1)/(1+g))^alpha*L^(-alpha);
K+C = (K(-1)/(1+g))*(1-delta)
+A*(K(-1)/(1+g))^alpha*L^(1-alpha);
log(A) = lambda*log(A(-1))-e;
End;

2 Kommentare

To use Dynare you should put the code into a .mod file. To run the code you would go to the command line and
dynare FILENAMEGOESHERE.mod
That will cause the .mod file to be pre-processed into MATLAB and the MATLAB would then be executed
Should the
+A*(K(-1)/(1+g))^alpha*L^(1-alpha);
line perhaps be on the end of the previous line?
I do not know anything about line continuation in that language.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by