Error: Invalid expression log equation

I am trying to solve an equation in Matlab mobile.
Following is the code -
syms t a
eqns =-a*log((3*t/4)*(3-t^2)/(2-t))-(1-a)*log((1/t^2)*((3-2*t)/(2-t)))+a*((1-t)^2)*(3-t)/((2-t)*(3-t^2)-(1-a)*(1+t)/(3-2*t))*((t*(-18+21*t-7*t^3+2*t^4)*(log((3-2*t)*(3-t^2)/(2*t(2-t)^2))))/(36-39*t+13*t^3-4*t^4+a*(-18+27*t-18*t^2+5*t^3)))+2*log((1/3)*(3-t^2)/(2-t))+t*log((4*(t^3)/27)*((3-t^2)/(3-2*t)))-2*t==0;
But I keep getting the error
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.

 採用された回答

Walter Roberson
Walter Roberson 2021 年 4 月 17 日

1 投票

syms t a
eqns =-a*log((3*t/4)*(3-t^2)/(2-t))-(1-a)*log((1/t^2)*((3-2*t)/(2-t)))+a*((1-t)^2)*(3-t)/((2-t)*(3-t^2)-(1-a)*(1+t)/(3-2*t))*((t*(-18+21*t-7*t^3+2*t^4)*(log((3-2*t)*(3-t^2)/(2*t(2-t)^2))))/(36-39*t+13*t^3-4*t^4+a*(-18+27*t-18*t^2+5*t^3)))+2*log((1/3)*(3-t^2)/(2-t))+t*log((4*(t^3)/27)*((3-t^2)/(3-2*t)))-2*t==0;
Error using sym/subsindex (line 864)
Invalid indexing or function definition. Indexing must follow MATLAB indexing. Function arguments must be symbolic variables, and function body must be sym expression.

Error in sym/subsref (line 909)
R_tilde = builtin('subsref',L_tilde,Idx);
% 12 1 2 1 2 10 1 0 12 1 23 2 3 210 12 1 0 1 0 12 1 2 1 2 1 2 1 2 10 12 3 2 3 45 4 5 4 5 6 5 4321 2 3 210 12 1 2 1 2 10 12 3 2 1 23 2 3 210
The bracket count is okay, it all balances out, and the error message you indicate does not occur. A different error occurs instead.
Have a look at the place the bracket count reaches 6:
% (2*t(2-t)^2)
% 5 6 5 4
Notice that you are trying to index t by 2-t . That is an error. You probably want (2*t*(2-t)^2)

15 件のコメント

Sabrina Garland
Sabrina Garland 2021 年 4 月 17 日
編集済み: Sabrina Garland 2021 年 4 月 17 日
Thanks @Walter. It helped!
I wrote this for solution but i got the following error -
Warning: Solutions are parameterized by the symbols: [z, z1, z2], z1, z2. To include parameters and conditions in the solution, specify the 'ReturnConditions' value as 'true'. > In sym/solve>warnIfParams (line 475) In sym/solve (line 357) Warning: Solutions are only valid under certain conditions. To include parameters and conditions in the solution, specify the 'ReturnConditions' value as 'true'. > In sym/solve>warnIfParams (line 478) In sym/solve (line 357)
Is there a problem with following syntax?
Soln1= solve(eqn, t, a)
And how can fix domain for t and a between 0 and 1 for solution?
Thanks!
Walter Roberson
Walter Roberson 2021 年 4 月 17 日
In theory....
syms t a real
assumeAlso(t >= 0 & t <= 1)
assumeAlso(a >= 0 & a <= 1)
eqn =-a*log((3*t/4)*(3-t^2)/(2-t))-(1-a)*log((1/t^2)*((3-2*t)/(2-t)))+a*((1-t)^2)*(3-t)/((2-t)*(3-t^2)-(1-a)*(1+t)/(3-2*t))*((t*(-18+21*t-7*t^3+2*t^4)*(log((3-2*t)*(3-t^2)/(2*t*(2-t)^2))))/(36-39*t+13*t^3-4*t^4+a*(-18+27*t-18*t^2+5*t^3)))+2*log((1/3)*(3-t^2)/(2-t))+t*log((4*(t^3)/27)*((3-t^2)/(3-2*t)))-2*t==0;
Soln1 = solve(eqn, t, a, 'returnconditions', true, 'maxdegree', 3)
Soln1.a
Soln1.t
Soln1.conditions
In practice, this is going to take a long time to compute, and the answers are probably going to be difficult to understand.
I will see if I can figure out anything more useful.
Walter Roberson
Walter Roberson 2021 年 4 月 17 日
Numerically, solutions include ones near
[
[0.80814734211076188233189764141161, 0.014157347318462409368745988278694]
[0.81774556051925063803415290145514, 0.017065493529859707439482572850095]
[0.82046261258401125142952904134027, 0.017914365002905370826371367553424]
[0.83165113247410330040663713139042, 0.021511344137270273069885390156103]
[0.86235181325356451804474711062975, 0.032006454691267775967023011531556]
[0.87728607400691739688823315300636, 0.037335270904563136299631940445499]
[0.89107163976355238503111378722198, 0.04233730158150518105730901834881]
[0.89922319441534142597544034193216, 0.045323008878958357652554141689136]
[0.92700631525224144351158031340986, 0.055600120484060467263409966499192]
[0.93050016660009881345983008776409, 0.056899680092455104708862984031671]
[0.94723947876611726467967480882978, 0.063136374364404389516159540203479]
[0.99119633032880843853815771634112, 0.079505614940861258639688187409334]
]
It is worth checking the accuracy of some of those: for example are the 2nd and 3rd really the same solution, or are they distinct solutions?
Walter Roberson
Walter Roberson 2021 年 4 月 17 日
The computation took about 3/4 of an hour and produced 33 possible solutions. Now I am seeing if I can make any sense out of the conditions.
Sabrina Garland
Sabrina Garland 2021 年 4 月 17 日
Thank you so much! That was of great help. It solved my problem!!! I am not sure if i can thank you enough.
Walter Roberson
Walter Roberson 2021 年 4 月 17 日
編集済み: Walter Roberson 2021 年 4 月 17 日
So... take
syms t a real
eqn =-a*log((3*t/4)*(3-t^2)/(2-t))-(1-a)*log((1/t^2)*((3-2*t)/(2-t)))+a*((1-t)^2)*(3-t)/((2-t)*(3-t^2)-(1-a)*(1+t)/(3-2*t))*((t*(-18+21*t-7*t^3+2*t^4)*(log((3-2*t)*(3-t^2)/(2*t*(2-t)^2))))/(36-39*t+13*t^3-4*t^4+a*(-18+27*t-18*t^2+5*t^3)))+2*log((1/3)*(3-t^2)/(2-t))+t*log((4*(t^3)/27)*((3-t^2)/(3-2*t)))-2*t;
fsurf(eqn, [0 1 0 1]);
view(3)
As you look closer in to this, it turns out that are there are an infinite number of solutions along a curved line. The limits are at approximately t < 0.1 and a > 0.7 and that otherwise you should be able to find a solution. For example,
t1 = 0.0123;
eqn1 = subs(eqn, t, t1)
eqn1 = 
a1 = vpasolve(eqn1)
a1 = 
Sabrina Garland
Sabrina Garland 2021 年 4 月 19 日
@Walter Sorry to disturb you again but I keep getting solution in parameters. Why is that?
Soln1 =
struct with fields:
t: [1×1 sym]
a: [1×1 sym]
parameters: [1×2 sym]
conditions: [1×1 sym]
Solution for a is coming out to be z1. Why?
Walter Roberson
Walter Roberson 2021 年 4 月 19 日
I demonstrated above that the solution is not a point or finite set of points, but is instead a line. As such, if MATLAB is going to be able to produce an answer at all, it would have to parameterize the line in some variable, and say "t has this relationship to the parameter, and a has this other relationship to the parameter."
For example:
syms x y real
sol = solve(x^2 + y^2 == 4, [x, y], 'returnconditions', true)
sol = struct with fields:
x: [2×1 sym] y: [2×1 sym] parameters: [1×1 sym] conditions: [2×1 sym]
sol.x
ans = 
sol.y
ans = 
sol.conditions
ans = 
MATLAB had to parameterize the solution because there are an infinite number of solutions.
Sabrina Garland
Sabrina Garland 2021 年 4 月 20 日
Oh! Thanks at Walter! Then how did you get the vector of numerical solutions that you printed? (I am new to Matlab and don't know the exact syntax for this).
Walter Roberson
Walter Roberson 2021 年 4 月 20 日
I used vpasolve with different random starting points, rand(2,1) and threw away results that did not meet the constraints.
Sabrina Garland
Sabrina Garland 2021 年 4 月 20 日
Okay! Thanks again!!!
Tomas
Tomas 2023 年 5 月 2 日
編集済み: Tomas 2023 年 5 月 2 日
I'm also getting a similar errror in a code that I am trying to write. I have tried writing the equation in about three or four different ways and I'm still getting the same error. The expression and error are shown below.Again this is just one of the forms that I've tried for the equation. I've tried -1.*log(2-2u) and -log(2-2u). Any help is appreciated!
f_inv = @x log(-2(2-u))
f_inv = @x log(-2(2-u))
Invalid expression. Check for missing multiplication operator,
missing or unbalanced delimiters, or other syntax error. To
construct matrices, use brackets instead of parentheses.
Walter Roberson
Walter Roberson 2023 年 5 月 2 日
-2(2-u)
There is absolutely no where in MATLAB that supports implicit multiplication. -2(2-u) as far as MATLAB is concerned, is unary minus applied to the result of indexing the number 2 by the index 2-u -- which is an error because literals cannot be subscripted in MATLAB.
Tomas
Tomas 2023 年 5 月 2 日
the U in this case is a random variable. So, should I just change it to a different random variable to clear that issue?
Walter Roberson
Walter Roberson 2023 年 5 月 2 日
f_inv = @(x) log(-2*(2-x))
This will be complex-valued unless x >= 2.
It is not at all clear why you do not instead write
f_inv = @(x) log(2*(x-2))

サインインしてコメントする。

その他の回答 (0 件)

製品

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by