0

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?

cspolton
  • 4,495
  • 4
  • 26
  • 34
  • 1
    I AM impressed that you got all of that into one single sentence. But what have you done? If you don't know how to use dsolve, then read the help for it. If you don't know how to get input from the user, then it is time to start reading the tutorials. You will get more help here if you show that you have made some effort. –  Sep 06 '12 at 16:23
  • thanks but I read the help for Dsolve and input functions this is on part of my program : 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]); I know that 'y(0)=a' and 'Dy(0)=b' is not correct with this syntax and that is my problem how can I do that?? – user1652400 Sep 07 '12 at 06:32

2 Answers2

0

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.

Yamaneko
  • 3,433
  • 2
  • 38
  • 57
0

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].

Christopher Creutzig
  • 8,656
  • 35
  • 45