How to convert a sym variable to an ordinary variable?
14 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
Walter Roberson
2022-9-26
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 个评论
Walter Roberson
2022-9-26
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'});
更多回答(1 个)
Chunru
2022-9-26
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
% 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
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!