Converting symbolic variables to numeric
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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!
0 Kommentare
Antworten (1)
Star Strider
am 3 Sep. 2018
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 Kommentare
Star Strider
am 3 Sep. 2018
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.
Siehe auch
Kategorien
Mehr zu Numbers and Precision finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!