Trouble converting Syms for plotting - Plots not accepting values as input

9 views (last 30 days)
Hi,
I have a model which uses a number (50-100) of syms, the syms are used to form equations (different models).
The syms are then converted in to values (doubles) and the equations plotted.
To convert the syms to values I have tried subs(), vpa(), and a few other forum solutions, and I think this is where the issue lies, however I am unable to use the converted values as inputs in plot functions (plot(), surf(), etc...).
Below is a short version of code which generates the problem, The resulting command window error is also included.
Thanks for any assistance
%*******CODE********
clear;
close all;
clc;
%Generate Symbol
syms POS;
%Convert Symbol to value
POS = 5;
POS = subs(POS);
%Variable for Plotting
A = POS
B = 5;
C = 10;
[x,y,z] = sphere(50);
%Plot Function
figure;
surf(x+A, y+B, z+C);
%*******Command Window********
>> test2
A = 5
Error using surf (line 57)
Invalid parameter/value pair arguments.
Error in test2 (line 11)
surf(x+A, y+B, z+C);
*Note; A is a 1x1 sym in the workspace

Accepted Answer

Walter Roberson
Walter Roberson on 28 Sep 2018
surf(double(x+A), double(y+B), double(z+C));
In your actual code, you will not be able to use double() here if symvar(A) or symvar(B) or symvar(C) show any symbols.
  3 Comments
Cams
Cams on 28 Sep 2018
Edited: Cams on 28 Sep 2018
For whatever reason this fails:
POS = 5;
POS = double(subs(POS));
Yet this works:
POS = 5;
POS = subs(POS);
POS = double(POS);
Walter Roberson
Walter Roberson on 28 Sep 2018
The output of subs() is always sym or perhaps symfun . You would not normally apply subs() to a numeric value: you would normally apply it to a symbolic expression. When you have
syms POS
then that is the same as
POS = sym('POS');
which creates a link into the symbolic engine workspace and stores it in POS in the MATLAB workspace.
When you then do
POS = 5;
then that does not affect the symbolic engine workspace at all, but instead overwrites that previous link with the numeric value 5.
If you happened to have something in the middle that used POS, like
syms POS
y = (POS-5)^2
and went on to
POS = 5;
then that is the same as
POS = sym('POS');
y = (POS-5)^2
POS = 5;
The first line creates a link into the symbolic engine workspace and stores it in POS in the MATLAB workspace . The second line looks in the MATLAB workspace at POS, finds the link to the symbolic engine, and ends up creating a symbolic expression that gets held in the symbolic engine workspace and a link to it is stored in y . The third line overwrites the link stored in POS, but does not affect the symbolic engine workspace. So the link that is stored in y still refers to the symbolic engine workspace that has a reference to a symbolic engine workspace variable named POS, but that POS is now unrelated to the POS stored at the MATLAB level that is now a numeric 5.
If you then try to double(y) then the POS referred to by y is the one in the symbolic engine workspace, which has no value, so you get an error. You need to
subs(y)
to have the POS in the symbolic engine matched to the POS in the MATLAB workspace by name and the numeric value substituted in.
My recommendation to avoid these kinds of problems is to never assign a value to a symbolic variable that has been used in an expression -- not unless you are intending to overwrite all of the expressions that referred to the old value.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!