Matlab code for solving and plotting function x'(t) = 1 + t*sin(t*x)

In summary, The function x'(t) = 1 + t*sin(t*x) where x(0) = 0 and t_final = 1 is being solved and plotted in Matlab in order to compare the exact solution to the approximations of Euler's and Improved Euler's Method. The user has tried using dsolve() but encountered an error, and has found that numerical solvers such as ode15s can provide a solution that is very close to the correct one. The user is unsure of the differences between numerical solvers and typically uses ode15s for similar problems.
  • #1
bdoherty1994
4
0

Homework Statement



I am trying to solve and plot the function, x'(t) = 1 + t*sin(t*x) where x(0) = 0 and t_final = 1, in order to compare this exact solution to the approximations of Euler's and Improved Euler's Method. Can anyone help me with the code in order to solve this problem, and then plot it, using Matlab?

Homework Equations



x'(t) = 1 + t*sin(t*x) where x(0) = 0 and t_final = 1

The Attempt at a Solution



I have tried to use dsolve() only to come up with an error.
 
Physics news on Phys.org
  • #2
wolfram alpha can't solve it and MATLAB tells me it can't find a solution either so I'm guessing you can't solve it algebraically. If you use a numerical solver on MATLAB the solution you get will be so close to the correct solution that if you knew the solution you wouldn't be able to tell the difference, so I'd just use that
 
  • #3
Thank you for your help! Which numerical solver would you recommend?
 
  • #4
I don't really understand the differences between them (they are designed to solve much harder problems than this one using varying techniques). I typically use this one
http://www.mathworks.com/help/matlab/ref/ode15s.html

but only because it's the one that I have seen used in examples and thus is the one I remember when I have to do something like this. Someone else could better explain what the advantages/disadvantages of each are perhaps, but it doesn't matter to this problem
 

FAQ: Matlab code for solving and plotting function x'(t) = 1 + t*sin(t*x)

What is Matlab code?

Matlab code is a programming language used for numerical computations and data visualization. It is commonly used in scientific and engineering fields for data analysis, modeling, and simulation.

How do I solve a function in Matlab?

To solve a function in Matlab, you can use the "solve" function. For the given function x'(t) = 1 + t*sin(t*x), the code would be:
syms x t
eqn = diff(x,t) == 1 + t*sin(t*x);
sol = solve(eqn,x);

How do I plot a function in Matlab?

To plot a function in Matlab, you can use the "plot" function. For the given function x'(t) = 1 + t*sin(t*x), the code would be:
t = linspace(-5,5);
x = sol(t);
plot(t,x);

How do I change the plot style in Matlab?

To change the plot style in Matlab, you can use the "plot" function with additional arguments. For example, to plot the function with a red dashed line, the code would be:
plot(t,x,'r--');

How do I save a plot in Matlab?

To save a plot in Matlab, you can use the "saveas" function. For example, to save the plot as a PNG file, the code would be:
saveas(gcf,'plot.png');

Similar threads

Replies
10
Views
1K
Replies
8
Views
846
Replies
1
Views
1K
Replies
3
Views
1K
Replies
1
Views
1K
Back
Top