How to solve a symbolic script expression within a cell and assign the answer to a new variable
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
I have a 1x1 symbolic object (X) containing the following symbolic expression:
int(T - 2*t, t, 0, T)
I want to create a new variable (a0) that is simply the result of the above integration.
I can obtain the result by calling out the symbolic expression in the command window, copying and pasting it back into the command window and hitting enter (answer = 0).
How do I do this without copy and pasting?
Thanks
Accepted Answer
Star Strider
on 28 Sep 2014
Edited: Star Strider
on 28 Sep 2014
Assign it to a variable:
syms t T
a0 = int(T - 2*t, t, 0, T)
produces:
a0 =
0
9 Comments
Nate
on 28 Sep 2014
Understand-
But I don't want to have to search for and type "int(T - 2*t, t, 0, T)" - as it will be a part of a more complex loop.
I need something in the form of:
a0 = solve(X)
(Where X is a 1x1 symbolic object containing a single cell with the symbolic expression "int(T - 2*t, t, 0, T)" )
But that doesn't work, of course. I can't find the right function to call out the symbolic expression contained within X and "run" it as if hitting "enter" in command window after simply copy and pasting. I hope that makes sense.
Thanks again
I’m not certain that I understand, but symfun might come to the rescue again:
syms t T T1 T2
a0 = symfun(int(T - 2*t, t, T1, T2), [T1 T2]);
Va0 = a0(0,T)
producing:
Va0 =
0
with ‘a0’ now being the function, and in this instance, ‘Va0’ evaluating it with the desired limits of integration. The Symbolic Math Toolbox evaluates the symbolic expression in the process, so that internally:
a0(T1, T2) =
(T1 - T2)*(T1 - T + T2)
Since I’m not certain that I understand everything in your Comment, a bit on solve as well:
The solve function will give you solutions in one of two formats, depending on what you ask for. If you ask it for more than one solution for an expression but give it only one output, it will produce a structure ‘a0’ with fields for each solution, for example ‘C’ and ‘D’:
a0 = solve(X)
it will produce ‘a0.C’ and ‘a0.D’. You can then define ‘C’ and ‘D’ as:
C = a0.C
D = a0.D
If you solve for and ask it specifically for ‘C’ and ‘D’, it will provide them:
[C,D] = solve(X)
assuming of course that it can find a solution.
Thanks again - I will clarify:
X is a 1x1 symbolic object containing a symbolic expression "int(T - 2*t, t, 0, T)"
I am asking for a method to simply extract the contents of X and solve the symbolic expression (ie: do the integration). I want this automated such that I do not have to search for "int(T - 2*t, t, 0, T)" within X and manually copy it to a new variable.
Eg:
Step 1.
X returns
X = int(T - 2*t, t, 0, T)
Step 2.
int(T - 2*t, t, 0, T) returns
ans = 0
I want to skip having to do the second step such that:
unknownfunction(X) returns
ans = 0
What is the unknownfunction that will do this?
Thanks again
If you only want to evaluate ‘X’ as those limits and store it as a variable, my original Answer lets you do just that. With ‘a0’ as I wrote it, the Toolbox solves and evaluates it and stores the result in ‘a0’. Replace my ‘a0’ with ‘X’ in your last Comment and you have the same result.
I have no idea what ‘unknownfunction’ does, other than to be the default behaviour of the Symbolic Math Toolbox.
I will give example code, note this is a different function:
clear all
syms T t positive real
syms a0 A0
x(T,t)= (t-floor(t/T)*T);
a0 = int(x(T,t),t,0,T);
A0 = simplify(a0,'IgnoreAnalyticConstraints',true)
Output:
A0 = int(t, t, 0, T)
BUT: If I copy and paste "int(t, t, 0, T)" into command window and hit enter:
ans = T^2/2
What is the shortcut to creating a new variable whose value is the symbolic solution to the integral expression in the output of the symbolic 1x1 object A0 (ie. whose value in this case is simply "T^2/2" and not "int(T - 2*t, t, 0, T)")
Nate
on 28 Sep 2014
Found it.
It's simply
eval(X)
Thanks
When I run your code, I get:
A0 =
T^2/2
I’m lost. It seems to be doing exactly what it should do.
You may have to ask it to simplify expressions (sometimes over several steps, using the 'steps' option in simplify) to get the answer. That’s just the nature of the MuPAD engine.
Nate
on 28 Sep 2014
Strange - I have R2014a. A0 returns the long form with int() for me, whereas eval(A0) returns T^2/2. All good, thanks
My pleasure!
I have R2014a as well. I have no idea what the difference in output could be due to.
More Answers (0)
Categories
Find more on Special Values in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)