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.
Parse error at '=':usage might be invalid MATLAB syntax

댓글 수: 9

Stephen23
Stephen23 2020년 2월 25일
편집: Stephen23 2020년 2월 25일
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?
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?
Dear Walter,
Yes I try to solve two iterative equations throug "characteristic method" that is a numeric method of solving non linear problems. It is a function. If you see at the parameters that I put into the function, I send all the information that I need to the equations to be solved (it is that I think, but I may have forgotten something or made a mistake).
Dear Stephen,
Why do you say that is an invalid sintax? Could you explain me it? I try to do an iterative loop to fill a matrix to solve a characteristic method.How would you do it?
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 ;)
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.
Dear Walter,
Thank you so much for your kind attention for my questions. I will try your solutions to my problem.
If I can't do it with Matlab, I will try to do it with C++ code.
Thank you again,
You are welcome,
Pablo
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.
Dear Walter,
Thank you again for your kind attention and soon answer to my questions.
I haven't programmed in matlab for a long time and I do this mistakes.
I will write my C code and I will share with you it to try rewrite it in Matlab code.
Thank you again.
Pablo

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

도움말 센터File Exchange에서 Function Creation에 대해 자세히 알아보기

질문:

2020년 2월 25일

댓글:

2020년 3월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by