How to convert a sym variable to an ordinary variable?

18 ビュー (過去 30 日間)
Roosevelt
Roosevelt 2022 年 9 月 26 日
コメント済み: Roosevelt 2022 年 9 月 27 日
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!

採用された回答

Walter Roberson
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
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'});
Roosevelt
Roosevelt 2022 年 9 月 27 日
Thank you! this led me to fix up my code and get a desired result. I really appreciate your help!

サインインしてコメントする。

その他の回答 (1 件)

Chunru
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
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 件のコメント
Roosevelt
Roosevelt 2022 年 9 月 26 日
Hello, thanks for responding. Is there a way to keep the graphs the same when doing the conversion?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeConversion Between Symbolic and Numeric についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by