I have a questions very similar to this post. Unfortunately there is no answer for the question and I have additional questions.
I am trying to generate a solve for the current in an RLC circuit and have the following second order differential equation that I would like to solve.
I have no issues with using the dsolve function to get a solution but when i try to graph the result. I find that I get errors that prevent the plot from generating. Furthermore, the errors that are stated conflict with eachother so im not able to understand where the issues is. As I'll show below, some errors state that the data is not in a numerical format and by making a change to the plot function (using fplot or fplot3). It suddenly believes that the data is a double which should be a numerical type but still throws an error.
I have tried 4 different sets of code.
Attempt 1 Code: Adapted from YouTube Video @ 9:19
syms I(t), R, L, C
DI=diff(I);
D2I=diff(DI);
eqn = d2I(t) + (R/L)*dI(t) + I(t)/(L*C) == 0;
cond = I(0) == 1, dI(t) == -6;
IT = dsolve(equ, cond);
tt=linspace(0, 2, 1000);
xx=subs(IT, tt);
figure(1);clf reset
plot(tt, IT, 'b');grid
Attempt 1 Error:
Error using plot
Data must be numeric, datetime, duration, categorical,
or an array convertible to double.
Error in untitled (line 25)
plot(tt, IT, 'b');grid
Attempt 2 Code: Saw a comment that suggested they use fplot instead so I tried the same.
syms I(t), R, L, C
DI=diff(I);
D2I=diff(DI);
eqn = d2I(t) + (R/L)*dI(t) + I(t)/(L*C) == 0;
cond = I(0) == 1, dI(t) == -6;
IT = dsolve(equ, cond);
tt=linspace(0, 2, 1000);
xx=subs(IT, tt);
figure(1);clf reset
fplot(tt, IT, 'b');grid
Attempt 2 Error:
Error using fplot>singleFplot
Input must be a function or functions of a single
variable.
Error in fplot>@(f1,f2)singleFplot(cax,{f1,f2},limits,extraOpts,args) (line 208)
hObj = cellfun(@(f1,f2) singleFplot(cax,{f1,f2},limits,extraOpts,args),fn{1},fn{2},'UniformOutput',false);
Error in fplot>vectorizeFplot (line 208)
hObj = cellfun(@(f1,f2) singleFplot(cax,{f1,f2},limits,extraOpts,args),fn{1},fn{2},'UniformOutput',false);
Error in fplot (line 166)
hObj = vectorizeFplot(cax,fn,limits,extraOpts,args);
Error in untitled (line 25)
fplot(tt, IT, 'b');grid
Attempt 3 Code: Could not find the page where i saw a suggestion to use fplot3 but it was on the Mathworks fourms.
syms I(t), R, L, C
DI=diff(I);
D2I=diff(DI);
eqn = d2I(t) + (R/L)*dI(t) + I(t)/(L*C) == 0;
cond = I(0) == 1, dI(t) == -6;
IT = dsolve(equ, cond);
tt=linspace(0, 2, 1000);
xx=subs(IT, tt);
figure(1);clf reset
fplot3(tt, IT, 'b');grid
Attempt 3 Error:
Error using fplot3
Expected input to be one of these types:
function_handle, sym
Instead its type was double.
Error in untitled (line 25)
fplot3(tt, IT, 'b');grid
Attempt 4 Code: Adapted from here
t_vector = linspace(0, 1800, 600);
syms I(t), R, L, C
dI(t) = diff(I(t))
d2I(t) = diff(dI(t))
eqn = d2I(t) + (R/L)*dI(t) + I(t)/(L*C) == 0;
cond = I(0) == 1, dI(t) == -6;
s(t) = dsolve(eqn, cond);
fplot(@(t) s(t), [t_vector(1), t_vector(end)])
Attempt 4 Error:
Warning: Error updating FunctionLine.
The following error was reported evaluating the
function in FunctionLine update: Unable to convert
expression containing symbolic variables into double
array. Apply 'subs' function first to substitute
values for variables.
As I mentioned above, there is a conflict between the error messages I am receiving between attempts 1 and 3 where attempts 1 states that it must be one of the listed options, Numerical being the type I expect; and attempt 3 where it states that the data is already a double which should be in numerical format.
I am looking for the following help.
- How can I plot the output function with a specified x-axis range?
- Why is there this conflict between attempts 1 and 3?
