Converting symbolic variables to numeric

Greetings! I'm having a bit of trouble with my code and I'm not sure how to figure it out. I've done some Googling, but haven't quite found someone with the same errors that I'm having. The essence of the problem is converting a symbolic expression into a numeric one that could be plotted.
Here is my code:
P1 = [-1.5, -2]
P2 = [2, 2]
P3 = [-2.5, 2.5]
P4 = [2, -1]
syms x
syms y
c = 299792.458e3
r1i = sqrt((P1(1,1) - x)^2 + (P1(1,2) - y)^2)
r2i = sqrt((P2(1,1) - x)^2 + (P2(1,2) - y)^2)
t21 = -3.7294e-6
S = double(solve(t21 == (r2i-r1i)/c, y))
This produces the error:
Error using symengine
DOUBLE cannot convert the input expression into a double array.
Error in sym/double (line 613)
Xstr = mupadmex('symobj::double', S.s, 0);
Error (line 18)
S = double(solve(sym(t21) == (r2i-r1i)/c, y))
I'm not quite sure what I'm doing wrong or what I should be doing differently. I would like to plot S, but until I convert it from a symbolic to a numeric, I don't believe I am able. Would anyone be able to provide any assistance? Thanks for your time and help in advanced, I really appreciate it!

Answers (1)

Your ‘S’ solution is a function of ‘x’. If you want to create an anonymous function that you can use in numeric computations, try this:
...
S = solve(t21 == (r2i-r1i)/c, y)
Sfcn = matlabFunction(S)
producing:
Sfcn =
function_handle with value:
@(x)[x.*1.119990285507171e-5-1.319940815489591e24.*sqrt(x.*2.787593149816328e42-x.^2.*5.575186299632656e42+1.742260481954225e48).*3.208606809481993e-46-2.799975713767927e-6;x.*1.119990285507171e-5+1.319940815489591e24.*sqrt(x.*2.787593149816328e42-x.^2.*5.575186299632656e42+1.742260481954225e48).*3.208606809481993e-46-2.799975713767927e-6]
that with a bit of editing becomes:
Sfcn = @(x)[x.*1.119990285507171e-5-1.319940815489591e24.*sqrt(x.*2.787593149816328e42-x.^2.*5.575186299632656e42+1.742260481954225e48).*3.208606809481993e-46-2.799975713767927e-6;x.*1.119990285507171e-5+1.319940815489591e24.*sqrt(x.*2.787593149816328e42-x.^2.*5.575186299632656e42+1.742260481954225e48).*3.208606809481993e-46-2.799975713767927e-6];
Use it as you would any other function.

6 Comments

Thank you for taking the time to answer, I really appreciate it! I tried it, and it certainly gets rid of the error, but the problem is, how do I plot Sfcn? Doing
plot(Sfcn)
Produces the error:
Error using plot
Invalid first data argument
Is Sfcn still symbolic?
My pleasure.
It’s not still symbolic. It’s now a function handle that you must evaluate in order to plot its results.
Example
x = linspace(-5, 5);
figure
plot(x, Sfcn(x))
grid
y = Sfcn(x);
figure
plot(x, y(y>0))
grid
The first plots the positive and negative values of the function, and the second only the positive values to show details.
Using the first seemed to produce two straight lines. Shouldn't they be hyperbolas? Is the output just not big enough in that case?
They are hyperbolas. It is necessary to expand or transform the y-axis in order to see them as such. The variation in the y-value is several orders-of-magnitude less than the value of the function:
yxtr = [min(y,[],2) max(y,[],2)]
ydif = diff(yxtr,[],2)
yxtr =
-559.020257202979 -558.995662913333
558.995545314353 559.020257259545
ydif =
0.0245942896467568
0.0247119451919389
Oh okay, that makes sense and I guess I figured as such. Is there a good way to expand the y-axis in the plotting so that it fills out better? Or take what you did there and use those as the max/mins for the plot?
I would plot them each separately (perhaps using subplot) over the x-range of values that you want. If you use one row of ‘yxtr’ to define the values of ylim for example, the other part of the plot will not be visible. The plot function will scale the individual parts of the function values itself.
I would use subplot (as subplot(2,1,1) for the positive part and subplot(2,1,2) for the negative part) to plot both with respect to the same x-values.
Experiment to get the result you want.

Sign in to comment.

Asked:

on 3 Sep 2018

Commented:

on 3 Sep 2018

Community Treasure Hunt

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

Start Hunting!