How to convert a sym variable to an ordinary variable?

19 visualizaciones (últimos 30 días)
Roosevelt
Roosevelt el 26 de Sept. de 2022
Comentada: Roosevelt el 27 de Sept. de 2022
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!

Respuesta aceptada

Walter Roberson
Walter Roberson el 26 de Sept. de 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 comentarios
Walter Roberson
Walter Roberson el 26 de Sept. de 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;
Y = double(subs(y, t, T));
Y_e = double(subs(y_e, t, T));
Y_o = double(subs(y_o, t, T));
plot(T, Y, T, Y_e, T, Y_o);
legend({'y', 'y_e', 'y_o'});
Roosevelt
Roosevelt el 27 de Sept. de 2022
Thank you! this led me to fix up my code and get a desired result. I really appreciate your help!

Iniciar sesión para comentar.

Más respuestas (1)

Chunru
Chunru el 26 de Sept. de 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 comentario
Roosevelt
Roosevelt el 26 de Sept. de 2022
Hello, thanks for responding. Is there a way to keep the graphs the same when doing the conversion?

Iniciar sesión para comentar.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by