1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab code problem with differential equations

  1. Dec 19, 2016 #1
    1. The problem statement, all variables and given/known data
    For a following differential equation
    d^2y/dx^2-4y=(e^x)/x
     Find the solution using numerical methods

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

    3. The attempt at a solution
    Code (Matlab M):

    %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: Dec 19, 2016
  2. jcsd
  3. Dec 19, 2016 #2

    jedishrfu

    Staff: Mentor

    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.
     
  4. Dec 19, 2016 #3

    Delta²

    User Avatar
    Gold Member

    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: Dec 19, 2016
  5. Dec 19, 2016 #4

    jedishrfu

    Staff: Mentor

    Good catch that x*(ii-1) is clearly wrong as the program needs x(ii-1)
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Matlab code problem with differential equations
  1. Matlab code (Replies: 14)

Loading...