Solve dy/dx=sin(x) in Matlab using ode45

  • Thread starter 8614smith
  • Start date
  • Tags
    Matlab
In summary: Your Name]In summary, there was an issue with the code for solving dy/dx=sin(x) using ode45. The error message showed that the variable "x" was not defined in the function file. Two possible solutions were suggested: defining "x" within the function or passing it as an additional input to the function.
  • #1
8614smith
54
0

Homework Statement


use ode45 to solve dy/dx=sin(x)


Homework Equations





The Attempt at a Solution


here is my function file:
function xx=f(t,y)
xx=sin(x);

i have saved it as xx.m so that's not the problem,
in the command window i enter:
>> [t,y]=ode45('xx',[0:0.001:0.5],[0 1]);

? Undefined function or variable 'x'.

Error in ==> xx at 2
xx=sin(x);
Error in ==> odearguments at 109
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.

Error in ==> ode45 at 173
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...

i thought x is defined as the initial values of 0 and 1, what am i doing wrong??
 
Physics news on Phys.org
  • #2


Thank you for reaching out for help with your problem. Based on the error message you received, it seems like the issue lies within your function file. The variable "x" is not defined in your function, so when you try to use it in the sin function, the program does not know what "x" represents.

To solve this issue, you can either define "x" within your function or pass it as an additional input to the function. Here are two possible solutions:

Solution 1: Defining "x" within the function

function xx=f(t,y)
x = t; % defining x as the input variable t
xx=sin(x);

Solution 2: Passing "x" as an additional input to the function

function xx=f(t,y,x)
xx=sin(x);

And in the command window, you can call the function as follows:

>> [t,y]=ode45(@(t,y)f(t,y,x),[0:0.001:0.5],[0 1]);

I hope this helps to solve your problem. Good luck with your work!

 

Related to Solve dy/dx=sin(x) in Matlab using ode45

1. How do I input the differential equation dy/dx=sin(x) into Matlab?

To input a differential equation into Matlab, you will need to use the "ode45" function. This function takes in the equation, initial conditions, and a time span as inputs, and returns the numerical solution. In this case, the equation would be inputted as "dy/dx=@(x,y) sin(x)".

2. Can I use any other functions besides "ode45" to solve this differential equation in Matlab?

Yes, there are other functions in Matlab that can be used to solve differential equations, such as "ode23" and "ode113". However, "ode45" is often the recommended function as it is a combination of the "ode23" and "ode113" functions and provides a good balance between accuracy and efficiency.

3. How do I specify the initial conditions for the differential equation in Matlab?

The initial conditions can be specified as a vector input in the "ode45" function. For example, if the initial condition is y(0)=1, then the input would be "[0 1]". The first element represents the initial value of x, and the second element represents the initial value of y.

4. How can I plot the numerical solution of the differential equation in Matlab?

After using the "ode45" function to solve the differential equation, the output will be a vector containing the values of x and y at different time points. To plot this solution, you can use the "plot" function in Matlab and provide the vector of x and y values as inputs.

5. Can I change the time span for the numerical solution of the differential equation in Matlab?

Yes, you can specify the time span for the solution in the "ode45" function by providing a vector with the desired start and end times as inputs. For example, if you want the solution for the time range of 0 to 10, the input would be "[0 10]".

Similar threads

  • Calculus and Beyond Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
958
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • Introductory Physics Homework Help
Replies
5
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
4K
  • Advanced Physics Homework Help
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
Back
Top