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

조회 수: 4 (최근 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 CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by