how to plot solution of ODE eqution with out using fplot ?

3 ビュー (過去 30 日間)
tomer polsky
tomer polsky 2022 年 5 月 8 日
コメント済み: tomer polsky 2022 年 5 月 8 日
hi I want to solve a simple differential equation . I solved the equation and plotted the answer . How ever I ploted the answer using fplot .I want to plot using the command 'plot' or 'stem' , my question is it possible to convert V_Sol to double ? I tried typing double(V_Sol) but I get this error :
Error using symengine
Unable to convert expression containing symbolic variables into double array. Apply 'subs' function first to substitute values
for variables.
here is my code
% this code is used to show how to use ode in matlab, via a simple problem
% for serios RC circuit with DC supply
clc;clear al;clc;
syms V_c(t)
R=1e3; % value of R
C=1e-3; % value of C
U=5; % value of the DC supply
ode = diff(V_c)+(1/(R*C) )*V_c == (1/(R*C) )*U; % the kirhov voltage law in the RC circuit
cond = V_c(0) == 0; % intial condtions
V_Sol(t) = dsolve(ode,cond); %solve V_c(t)
fplot(V_Sol(t),[0,5]);% plot V_c(t)
xlabel('t[sec]');
ylabel('V_c(t)[v]');
title(' solution of : $\frac{dV_c(t)}{dt}*RC +V_c(t)=U $','Interpreter','latex','FontSize', 14);
grid on;
legend('V_c(t)');
% plot V_Sol using plot command
t=0:0.001:5;
subs t;
plot(t,double(V_Sol))

採用された回答

VBBV
VBBV 2022 年 5 月 8 日
% this code is used to show how to use ode in matlab, via a simple problem
% for serios RC circuit with DC supply
clc;clear al;clc;
syms V_c(t)
R=1e3; % value of R
C=1e-3; % value of C
U=5; % value of the DC supply
ode = diff(V_c)+(1/(R*C) )*V_c == (1/(R*C) )*U; % the kirhov voltage law in the RC circuit
cond = V_c(0) == 0; % intial condtions
V_Sol(t) = dsolve(ode,cond) %solve V_c(t)
V_Sol(t) = 
fplot(V_Sol(t),[0,5]);% plot V_c(t)
xlabel('t[sec]');
ylabel('V_c(t)[v]');
title(' solution of : $\frac{dV_c(t)}{dt}*RC +V_c(t)=U $','Interpreter','latex','FontSize', 14);
grid on;
legend('V_c(t)');
% plot V_Sol using plot command
T = 0:0.001:5;
%subs t;
plot(T,subs(V_Sol,t,0:0.001:5)) % dont use same variable for plotting, since t is symbolic
  2 件のコメント
VBBV
VBBV 2022 年 5 月 8 日
if you want to use same variable, you can use matlabFunction as @Torsten suggested.
% this code is used to show how to use ode in matlab, via a simple problem
% for serios RC circuit with DC supply
clc;clear al;clc;
syms V_c(t)
R=1e3; % value of R
C=1e-3; % value of C
U=5; % value of the DC supply
ode = diff(V_c)+(1/(R*C) )*V_c == (1/(R*C) )*U; % the kirhov voltage law in the RC circuit
cond = V_c(0) == 0; % intial condtions
V_Sol(t) = dsolve(ode,cond); %solve V_c(t)
fplot(V_Sol(t),[0,5]);% plot V_c(t)
xlabel('t[sec]');
ylabel('V_c(t)[v]');
title(' solution of : $\frac{dV_c(t)}{dt}*RC +V_c(t)=U $','Interpreter','latex','FontSize', 14);
grid on;
legend('V_c(t)');
% plot V_Sol using plot command
Vsol = matlabFunction(V_Sol);
t = 0:0.001:5;
plot(t,Vsol(t))
tomer polsky
tomer polsky 2022 年 5 月 8 日
thank you very much for you help !!!

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

その他の回答 (1 件)

Torsten
Torsten 2022 年 5 月 8 日
編集済み: Torsten 2022 年 5 月 8 日
Vsol = matlabFunction(VSol);
t = 0:0.001:5;
plot(t,Vsol(t))

カテゴリ

Help Center および File ExchangeFormula Manipulation and Simplification についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by