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.
You are trying to plot the symbolic solution IT
instead of the computed numerical values of the solution xx
. With the line/curve plotting command plot
this will always give an error, that function does not know how to handle symbolic expressions.
In your last example using fplot
you were almost correct, only the transformation of the symbolic function into an anonymous function was not necessary and irritated the interpreter. See the fplot
docs.
In-between, note that Matlab perpetuates the confusing kind of abuse of notation. In s(t) = dsolve(eqn, cond);
the letter t
is a place-holder, just denoting that s(t)
is a function of some variable. One line later, in fplot(@(t) s(t), [t_vector(1), t_vector(end)])
, this letter has two roles. In s(t)
it is again part of a compound symbol, in @(t)
it declares a local variable that is no further used. Despite the intuitive form of s(t)
, to get a value you have to use again the substitution command, @(tt) subs(s(t),tt)
. But this still defines a Matlab function, not an expression like s(t)
as expected by fplot
.