Suppose that you have a differential equation and you want to solve that with dsolve
function in matlab but at first you must ask the user for initial values and according to what he would input the program gives the answer.
How should I do that?
Suppose that you have a differential equation and you want to solve that with dsolve
function in matlab but at first you must ask the user for initial values and according to what he would input the program gives the answer.
How should I do that?
Do you want to know how to get the user input? Then, you may use the input()
function. Example:
reply = input('Do you want more? Y/N [Y]: ', 's');
where 's'
parameter means that the user's input will not be evaluated, i.e., the characters are simply returned as a MATLAB string. Maybe you want the user to input an expression to be solved by dsolve
. You can do something like:
expression = input('Which expression do you want to solve?','s');
dsolve(expression)
If the user inputs 'Dx = -a*x'
, then you will have dsolve('Dx = -a*x')
.
More information in the input()
web documentation.
You tried (according to your comment):
a=input('y(0) = ');
b=input('y''(0) = ');
c=input('input the first of the domain : ');
d=input('input the last of the domain : ');
sym x;
y=dsolve('D2y+Dy+y=cos(x)','y(0)=a','Dy(0)=b','x');
h=ezplot(y,[c d]);
The sym x
doesn't do anything, since you ignore the output. You can safely omit that.
Now, to get the user input into the dsolve
command, you have to write code that creates the corresponding string:
y=dsolve('D2y+Dy+y=cos(x)',['y(0)=' num2str(a)],['Dy(0)=' num2str(b)],'x');
Or, use input
with the 's'
flag and ['y(0)=' a]
.