How to convert a sym variable to an ordinary variable?

13 views (last 30 days)
Hello, I am trying to convert my code back to ordinary variables so I can use it in signal analyzer. The code is below
close all;
clear all;
clc;
t= linspace(-1,3);
syms x_t(t);
x_2(t) = piecewise(t<-1,(2),-1<t<=-.5,(t.*4+6),-.5<t<2, (-2.4*t+3),t==2,(2),t>=2, (2));
y = -x_2(-1-t)+1;
y_e=((-x_2(-1-t)+1)+(-x_2(1+t)+1))*.5;
y_o=((-x_2(-1-t)+1)-(-x_2(1+t)+1))*.5;
tiledlayout('flow')
nexttile
fplot(x_2)
title('Original Signal');
xlabel('time'); % label the horizontal (time) axis
ylabel('amplitude'); % label the vertical (x_t) axis
grid on;
nexttile
fplot(y)
title('Transform');
xlabel('time'); % label the horizontal (time) axis
ylabel('amplitude'); % label the vertical (x_t) axis
grid on;
nexttile
fplot(y_e)
title('Even');
xlabel('time'); % label the horizontal (time) axis
ylabel('amplitude'); % label the vertical (x_t) axis
grid on;
nexttile
fplot(y_o)
title('Odd');
xlabel('time'); % label the horizontal (time) axis
ylabel('amplitude'); % label the vertical (x_t) axis
grid on;
Any help is appreciated thank you!

Accepted Answer

Walter Roberson
Walter Roberson on 26 Sep 2022
symbolic variables can be converted to numeric only if they have no unbound variables and all expressions with bound variables (such as int() expressions) converge.
Your y* variables contain the unbound variable t and so cannot be converted to numeric.
However, you can subs() specific numeric values for the unbound variables and try to double() the result. That should work provided the expression converges.
  3 Comments
Roosevelt
Roosevelt on 27 Sep 2022
Thank you! this led me to fix up my code and get a desired result. I really appreciate your help!

Sign in to comment.

More Answers (1)

Chunru
Chunru on 26 Sep 2022
t= linspace(-1,3);
syms x_t(t);
x_2(t) = piecewise(t<-1,(2),-1<t<=-.5,(t.*4+6),-.5<t<2, (-2.4*t+3),t==2,(2),t>=2, (2));
y = -x_2(-1-t)+1;
y_e=((-x_2(-1-t)+1)+(-x_2(1+t)+1))*.5;
y_o=((-x_2(-1-t)+1)-(-x_2(1+t)+1))*.5;
tiledlayout('flow')
nexttile
fplot(x_2)
title('Original Signal');
xlabel('time'); % label the horizontal (time) axis
ylabel('amplitude'); % label the vertical (x_t) axis
grid on;
nexttile
fplot(y)
title('Transform');
xlabel('time'); % label the horizontal (time) axis
ylabel('amplitude'); % label the vertical (x_t) axis
grid on;
nexttile
fplot(y_e)
title('Even');
xlabel('time'); % label the horizontal (time) axis
ylabel('amplitude'); % label the vertical (x_t) axis
grid on;
nexttile
fplot(y_o)
title('Odd');
xlabel('time'); % label the horizontal (time) axis
ylabel('amplitude'); % label the vertical (x_t) axis
grid on;
whos
Name Size Bytes Class Attributes cmdout 1x33 66 char t 1x1 8 sym x_2 1x1 8 symfun x_t 1x1 8 symfun y 1x1 8 sym y_e 1x1 8 sym y_o 1x1 8 sym
% for example of y_e
y_e = symfun(y_e, t); % convert to symfunction
y_e = double(y_e(-5:.1:5)) % evaluate the function and convert to double
y_e = 1×101
-1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 0.7800 0.6600 0.5400 0.4200 0.3000 0.1800 0.0600 -0.0600 -0.1800
  1 Comment
Roosevelt
Roosevelt on 26 Sep 2022
Hello, thanks for responding. Is there a way to keep the graphs the same when doing the conversion?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!