How to convert a sym variable to an ordinary variable?
15 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Roosevelt
am 26 Sep. 2022
Kommentiert: Roosevelt
am 27 Sep. 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!
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 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 Kommentare
Walter Roberson
am 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;
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'});
Weitere Antworten (1)
Chunru
am 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
% 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
Siehe auch
Kategorien
Mehr zu Conversion Between Symbolic and Numeric 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!