MATLAB MATLAB- Creating Symbolic Equation Question

  • Thread starter Thread starter cookiemnstr510510
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary
To create symbolic equations in MATLAB, first define your variables using the "syms" command. When declaring equations, replace the equal sign '=' with '==' to avoid errors. For example, use "ideal_gas_law = P*V == n*R*Temp;" instead of using quotes. Additionally, you can define expressions directly without quotes, such as "E = m*c^2;" after defining "m" and "c" as symbolic variables. Understanding these rules will help avoid common errors in symbolic mathematics with MATLAB.
cookiemnstr510510
Messages
162
Reaction score
14
TL;DR
I am currently taking MATLAB. We are using the textbook: MATLAB for Engineers fourth edition by Holly Moore. I am in the symbolic mathematics section and am having some issues. I am using MATLAB R2018b
Hello All!
I am finding some inconsistencies with my textbook and MATLAB. I am in the section of symbolic mathematics.
I follow the textbook word for word and I am getting some errors. When I am trying to create an entire equation and give it a name like:
ideal_gas_law=sym('P*V=n*R*Temp') I get the error:

Error using sym>convertChar (line 1459)
Character vectors and strings in the first argument can only specify a variable or number.
To evaluate character vectors and strings representing symbolic expressions, use 'str2sym'.

Error in sym>tomupad (line 1225)
S = convertChar(x);

Error in sym (line 214)
S.s = tomupad(x);

Just by looking at this error I have no idea what it is trying to tell me, I tried this:

ideal_gas_law=str2sym(sym('P*V=n*R*Temp))

with no luck.

I looked online and saw some info about it, but reading that didn't clear anything up for me. I am wondering if someone could show me how to write an equation like the one above. What do I need to do? What am I not doing correctly? Any help is much appreciated. Programming is pretty difficult for me (and I know MATLAB is not considered programming), so if you could sort of spell things out for me as much as possible that would be best for my learning style.

Thank you
Happy Weekend!
 
Last edited by a moderator:
Physics news on Phys.org
First define the variables: syms P, V, n, R, Temp;

Then declare your equation, replacing the '=' within the equation by '==':
ideal_gas_law = P*V==n*R*Temp;
 
  • Like
Likes cookiemnstr510510
Wrichik Basu said:
First define the variables: syms P, V, n, R, Temp;

Then declare your equation, replacing the '=' within the equation by '==':
ideal_gas_law = P*V==n*R*Temp;
Thank you! Appreciate it a lot
 
Wrichik Basu said:
First define the variables: syms P, V, n, R, Temp;

Then declare your equation, replacing the '=' within the equation by '==':
ideal_gas_law = P*V==n*R*Temp;
One more question:
my textbook also says that the sum function can also be used to create either and entire expression or an entire equation. then gives the example of:
E=sym('m*c^2'); this also gives an error.
Using the above logic I tried to first define m and c like:
syms m c
then I tried
E=sym('m*c^2')
and also tried
E==sym('m*c^2')
neither worked...
Then i thought since we defined "m" and "c" as symbolic variables, we can just write:
E=m*c^2?
and this will define a new symbolic variable E that have the other two variables in it?

Sorry, still a bit confused.

Thanks!

Thank you!
 
cookiemnstr510510 said:
Then i thought since we defined "m" and "c" as symbolic variables, we can just write:
E=m*c^2?
and this will define a new symbolic variable E that have the other two variables in it?
Yes, that is how it works.

The rule of the thumb is, you have to define variables beforehand. Constants can be defined within the expression. For equations, the = within the equation will be replaced by ==. Don't use quotation('...') for defining symbolic expressions.

For example, this is correct:
Code:
syms x y z;
f = sin(sym(pi) - sym(1/3)) + tan(y*z/x);

Here is one article that can help you out:
https://in.mathworks.com/help/symbolic/create-symbolic-numbers-variables-and-expressions.html
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 14 ·
Replies
14
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K