Search code examples
matlabdsolve

Cannot transfer variable values into dsolve?


I got one question in matlab:

I get a value of c from other function, then I want to put c value 1 into dsolve to solve this differential equation. but the value cannot put into dsolve, what should I do to transfer value? eg:

c = 1; 
u = dsolve('Du = 1+u^c','t') % c is c, but is not 1!

Solution

  • The reason for your error is that when you use 'c', c is interpreted as a char (or string) and not a variable. What you could do is use num2str to combine strings and variables, like this:

    u = dsolve(['Du = 1+u^' num2str(c)],'t') % c is c, but is not 1!
    u =
    C16*exp(t) - 1
    

    If you have several variables, you can do something like this:

    u = dsolve(['Du = ' num2str(1/(m*n)) '*1 + u^' num2str(c)])
    u =
    C16*exp(t) - 1