Matlab code problem with differential equations

Click For Summary

Discussion Overview

The discussion revolves around a MATLAB coding problem related to solving a second-order differential equation numerically. Participants are focused on identifying and correcting errors in the provided code snippet, which implements a numerical method for the equation.

Discussion Character

  • Homework-related
  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • The original poster presents a differential equation and a MATLAB code snippet intended to solve it numerically, seeking assistance with an error encountered during execution.
  • One participant asks for clarification on the specific error message being received to better diagnose the issue.
  • Another participant suggests that the error may stem from the mathematical expression used in the code, specifically pointing out potential misplacement of parentheses in the exponential term.
  • This same participant also notes a possible typo in the indexing of the array x, indicating that it should be x(ii-1) instead of x*(ii-1).
  • A later reply agrees with the correction regarding the indexing of x and emphasizes the importance of careful attention to the code's mathematical expressions.

Areas of Agreement / Disagreement

Participants generally agree on the need to correct the indexing of the variable x in the code. However, there is no consensus on the specific nature of the error being encountered, as the original poster has not provided details on the error message.

Contextual Notes

There are unresolved aspects regarding the exact nature of the error in the MATLAB code, as well as the overall approach to the numerical method being used. The discussion does not clarify whether the proposed corrections will resolve the issue.

pugtm
Messages
17
Reaction score
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
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.
 
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:
Good catch that x*(ii-1) is clearly wrong as the program needs x(ii-1)
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K