parse error at '=': usage might be invalid matlab syntax
이전 댓글 표시
Dear colleagues,
I wrote the following code for a function, but when I run it, there is an error at Command Window that tells me the following message:
>> Metodo_Caracteristicas_2
Error: File: Metodo_Caracteristicas_2.m Line: 14 Column: 37
The expression to the left of the equals sign is not a valid target for an assignment.
I will would greatly appreciate who helps me, thank you.

댓글 수: 9
What do you expect this invalid syntax:
Q_I(i,n) + (1/zo)*P_I(i,n) = ...
Q_I(i,n) - (1/zo)*P_I(i,n) = ...
to do?
Walter Roberson
2020년 2월 25일
It sort of looks like you are trying to construct equations to be solved, except that you have nothing in the code that solves equations.
Are you trying to state a periodic boundary condition?
Pablo Silvoni
2020년 3월 2일
Pablo Silvoni
2020년 3월 2일
Walter Roberson
2020년 3월 2일
In MATLAB the = operator is nearly always reserved for assignment, and not for expressing equality. The == operator (two = instead of one) is mostly used for testing equality, but with the Symbolical Toolbox == has the important additional functionality of expressing equations.
Your code is attempting to use the assignment operator = between two expressions. That is rarely valid in MATLAB. The assignment operator = must have on its left something that designates a location to store into.
There are several different ways you could proceed.
Your current code has something of the form
Location1 + expression1 = Location2 + expression2
That could be rewritten as
Location1 = Location2 + expression2 - expression1
However if you do this then what you get is an assignment that changes what is stored at the location1, not a equation.
You could also write something of the form:
WasSame = Location1 + expression1 == Location2 + expression2
When the values involved are numeric the result would be a logical value, 0 (false) or 1 (true). The numeric test is for bit-for-bit equality, and due to roundoff effects most of the time you will not get that kind of exact equality. It is often a mistake to use == between floating point expressions.
You can also write something similar to
WasSame = abs((Location1 + expression1) - (Location2 + expression2)) < tolerance
This tests whether the two are "close" to being the same, to take into account round-off error.
There are additional important possibilities but I am tired of typing on my phone ;)
Walter Roberson
2020년 3월 2일
Also, you could write
equation1 = Location1 + expression1 == Location2 + expression2
but this time make there be symbolic variables involved. You would have a similar equation2 for your other line that is currently written with = in the middle of the line, and you could proceed from there to
solution = solve([equation1, equation2], [variable1, variable2])
to have it work out what values of variable1 and variable2 would be needed in order for both equations to be simultaneously true.
Or back on the numeric side, you might write something like
fun = @(parameters) [(Location1 + expression1_in_parameters) - (Location2 + expression2_in_parameters);
(Location3 + expression3_in_parameters) - (Location4 + expression4_in_parameters)];
after which you could fsolve(fun, initial_guess) in order to find the numeric values of the parameters that satisfies both conditions. This is not restricted to having the same number of expressions as you have parameters. In the above, the bit about Location3 and so on has to do with the second line you had in which you had = in the middle of an expression.
As you can see there are multiple approaches you can take.
When I look at your code, what looks to me to be the most likely fix is to move the (1/z0) part to the right hand side of the = making the appropriate adjustment to the sign.
Pablo Silvoni
2020년 3월 4일
Walter Roberson
2020년 3월 4일
Well, that kind of line with an expression and then "=" and then another expression, is valid in C only to the extent that the person is coding an inline assignment statement in a confusing manner. For example in C,
A + B = 1 + C;
is valid and is to be interpreted as:
calculate or retrieve A and pull it into memory
assign 1 to variable B and pull the resulting value into memory
do the addition of those two items in memory, leaving the result in memory
calculate or retrieve C and pull the resulting value into memory
do the addition of those two items in memory, leaving the result in memory
discard the result in memory
In your case, it would be P_I[i][n] that would be assigned into in both lines. It is unlikely that that would be what you would want to do.
If you can rewrite the lines the way you would code them in C, there is a good chance we could tell you how they would then be coded in MATLAB.
Pablo Silvoni
2020년 3월 4일
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Function Creation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!