MATLAB- Creating Symbolic Equation Question

  • MATLAB
  • Thread starter cookiemnstr510510
  • Start date
  • Tags
    Matlab
In summary, the conversation discusses the process of creating symbolic equations in MATLAB and the errors that can occur. The solution is to define the variables beforehand and use '==' instead of '=' within the equation. Quotation marks should not be used when defining symbolic expressions. Additional resources can be found online for further clarification.
  • #1
cookiemnstr510510
162
14
TL;DR Summary
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
  • #2
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
  • #3
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
 
  • #4
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!
 
  • #5
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
 

1. How do I create a symbolic equation in MATLAB?

To create a symbolic equation in MATLAB, you can use the "syms" command followed by the variables you want to use in your equation. For example, if you want to create an equation with the variables 'x' and 'y', you would type "syms x y" in the command window.

2. Can I solve a symbolic equation in MATLAB?

Yes, you can use the "solve" command in MATLAB to solve a symbolic equation. This command takes in the symbolic equation and the variable you want to solve for as inputs, and returns the solution.

3. How can I simplify a symbolic equation in MATLAB?

To simplify a symbolic equation in MATLAB, you can use the "simplify" command. This command takes in the equation as an input and returns a simplified version of the equation.

4. Is it possible to plot a symbolic equation in MATLAB?

Yes, you can plot a symbolic equation in MATLAB using the "ezplot" command. This command takes in the equation as an input and automatically generates a plot of the equation.

5. Can I convert a symbolic equation to a numerical equation in MATLAB?

Yes, you can use the "double" command in MATLAB to convert a symbolic equation to a numerical equation. This command takes in the symbolic equation as an input and returns a numerical version of the equation.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Back
Top