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

6 次查看(过去 30 天)
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))

请先登录,再进行评论。

更多回答(1 个)

Torsten
Torsten 2022-5-8
编辑:Torsten 2022-5-8
Vsol = matlabFunction(VSol);
t = 0:0.001:5;
plot(t,Vsol(t))

类别

Help CenterFile Exchange 中查找有关 Formula Manipulation and Simplification 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by