Matlab code problem with differential equations

In summary: And no no need to be rusty. Just read up on it.In summary, the conversation discusses finding a solution to a specific differential equation using numerical methods. The code provided uses the Euler Method to solve the ODE, but there is an error in the line that calculates ##m(ii)##, possibly due to incorrect parentheses and indexing.
  • #1
pugtm
18
0

Homework Statement


For a following differential equation
d^2y/dx^2-4y=(e^x)/x
 Find the solution using numerical methods

Homework Equations


d^2y/dx^2-4y=(e^x)/x

The Attempt at a Solution


Matlab:
%num
dx=0.01;
x=1:dx:3;
l=zeros(1,length(x));
m=zeros(1,length(x));
l(1)=1;
m(1)=0.25;
for ii=2:length(x)
    l(ii) = l(ii-1)+m(ii-1)*dx;
    m(ii) = m(ii-1) -(1/4) * (l(ii-1)) * dx +(exp(2*x*(ii-1)/(x(ii-1))))*dx;<----- this line keeps throwing an error
end
plot(x,l);
any assistance would be greatly appreciated
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
What error are you getting?

If its during execution then perhaps a print statement inside your for loop printing out the results of L(ii) and M(ii) and some of your expressions will give you some insight into what going on.

You should be able to use the computer to help you find the error in your code by tracing its operations.
 
  • #3
The way the code is it adds at every step ##e^{\frac{2x}{x}}dx## while I believe you want to add ##\frac{e^{2x}}{x}dx##. Check carefully the parentheses in the (exp(2*x)...) term at that line.

Also what it seems to be a typo it says exp(2*x*(ii-1)... while I believe it should be exp(2*x(ii-1))...

Also not sure since I am rusty on numerical methods and MATLAB code but this looks like the Euler Method for solving ODEs correct?
 
Last edited:
  • #4
Good catch that x*(ii-1) is clearly wrong as the program needs x(ii-1)
 

1. What is Matlab code for solving differential equations?

Matlab has built-in functions for solving differential equations, such as the ode45 and ode15s functions. These functions use numerical methods to approximate the solution to a given differential equation.

2. How do I define a differential equation in Matlab?

In Matlab, differential equations can be defined using anonymous functions. For example, if we have the differential equation y' = 2xy, we can define it as @(x,y) 2*x*y. This function can then be used in conjunction with the ode45 or ode15s functions to solve the equation.

3. What are common errors in Matlab code for differential equations?

Some common errors in Matlab code for differential equations include not specifying initial conditions or incorrect syntax in the differential equation definition. It is also important to check for any division by zero or other mathematical errors.

4. How can I plot the solution to a differential equation in Matlab?

To plot the solution to a differential equation in Matlab, use the ode45 or ode15s functions to solve the equation and then use the plot function to plot the solution. You can also use the deplot function to plot the direction field of the differential equation.

5. Can I use Matlab to solve partial differential equations?

Yes, Matlab has built-in functions for solving partial differential equations, such as the pdepe function. This function uses finite difference methods to solve partial differential equations. It is important to note that solving partial differential equations can be more computationally intensive and may require more advanced knowledge of Matlab programming.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
954
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
886
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
881
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
Back
Top