Results 1 to 11 of 11

Math Help - Matlab task

  1. #1
    Newbie
    Joined
    Oct 2008
    Posts
    24

    Matlab task

    Need to finish a program that approximates a first order differential equation with eulers method.


    Code:
    function [t,U]=minPrim(?,?,?,?)
    
    NrIn=nargin;
    if NrIn==?  
    
    elseif NrIn=?
      N=100;
    else
      error(’Wrong number of input arguments!’)
    end
    U=zeros(1,N+1);
    
    k=? %increment
    t=? %Vector with all t:s
    U(1)=U0;
    for k=2:?
      U(?)=U(?)+k*f(?);
    end
    4 input arguments

    f, function
    Vector with rand values (2 of them, this is the intervall)
    Initial value U0
    Number if intervalls N.


    This should solve for example:

    u'=x^2

    u0=3

    {x:0,2}
    Last edited by CaptainBlack; June 1st 2011 at 11:17 PM.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by techmath View Post
    Need to finish a program that approximates a first order differential equation with eulers method.


    Code:
    function [t,U]=minPrim(?,?,?,?)
    
    NrIn=nargin;
    if NrIn==?  
    
    elseif NrIn=?
      N=100;
    else
      error(’Wrong number of input arguments!’)
    end
    U=zeros(1,N+1);
    
    k=? %increment
    t=? %Vector with all t:s
    U(1)=U0;
    for k=2:?
      U(?)=U(?)+k*f(?);
    end
    4 input arguments

    f, function
    Vector with rand values (2 of them, this is the intervall)
    Initial value U0
    Number if intervalls N.


    This should solve for example:

    u'=x^2

    u0=3

    {x:0,2}
    So what have you done so far?

    CB
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Newbie
    Joined
    Oct 2008
    Posts
    24
    Code:
    function [t,U]=minPrim(f,[I0,I],U0,N)
    
    NrIn=nargin;
    if NrIn==?  
    
    elseif NrIn=?
      N=100;
    else
      error(’Wrong number of input arguments!’)
    end
    U=zeros(1,N+1);
    
    k=(I-I0)/N %increment
    t=I0:k:I %Vector with all t:s
    U(1)=U0;
    for k=2:?
      U(?)=U(?)+k*f(?);
    end

    This is what i have done so far. I dont know how to apply this in the loop.
    Because U dependes botn on the i:th intervall and the position t.

    A guess from my side is:

    for 2:k:N
    U(k)=U(k-1)+k*f(???)
    end
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by techmath View Post
    Code:
    function [t,U]=minPrim(f,[I0,I],U0,N)
    
    NrIn=nargin;
    if NrIn==?  
    
    elseif NrIn=?
      N=100;
    else
      error(’Wrong number of input arguments!’)
    end
    U=zeros(1,N+1);
    
    k=(I-I0)/N %increment
    t=I0:k:I %Vector with all t:s
    U(1)=U0;
    for k=2:?
      U(?)=U(?)+k*f(?);
    end

    This is what i have done so far. I dont know how to apply this in the loop.
    Because U dependes botn on the i:th intervall and the position t.

    A guess from my side is:

    for 2:k:N
    U(k)=U(k-1)+k*f(???)
    end

    ]
    Code:
    function [t,U]=minPrim(f,[I0,I],U0,N)
    
    NrIn=nargin;
    if NrIn==?  
    
    elseif NrIn=?
      N=100;
    else
      error(’Wrong number of input arguments!’)
    end
    U=zeros(1,N+1);
    
    k=(I-I0)/N %increment
    t=I0:k:I %Vector with all t:s
    U(1)=U0;
    for ik=2:length(U)
      U(ik)=U(ik-1)+k*f(t(ik-1),U(ik-1));
    end
    Assuming you have defined the derivative as U'(t)=f(t,U)

    CB
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Newbie
    Joined
    Oct 2008
    Posts
    24
    Yes that is correct.

    I dont know for example how it is possible to put a function f as argument. I thought it was only possible to but in values directly, for example:

    minPrim(1,2,3,4) instead of minPrim(x^2,2,3,4)
    Follow Math Help Forum on Facebook and Google+

  6. #6
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by techmath View Post
    Yes that is correct.

    I dont know for example how it is possible to put a function f as argument. I thought it was only possible to but in values directly, for example:

    minPrim(1,2,3,4) instead of minPrim(x^2,2,3,4)
    Research Matlab function handle Handle used in calling functions indirectly - MATLAB

    CB
    Follow Math Help Forum on Facebook and Google+

  7. #7
    Newbie
    Joined
    Oct 2008
    Posts
    24
    OK, i think i have to use it like this then:

    minPrim(@functionname,[I0,I],U0,N)

    So i make a new file with the function i want to use.

    But i have an error, it is at the first row. The vector [I0,I], maybe i didnt express it correctly...

    ??? Error: File: minPrim.m Line: 1 Column: 28
    Unbalanced or unexpected parenthesis or bracket.
    Follow Math Help Forum on Facebook and Google+

  8. #8
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by techmath View Post
    OK, i think i have to use it like this then:

    minPrim(@functionname,[I0,I],U0,N)

    So i make a new file with the function i want to use.

    But i have an error, it is at the first row. The vector [I0,I], maybe i didnt express it correctly...

    ??? Error: File: minPrim.m Line: 1 Column: 28
    Unbalanced or unexpected parenthesis or bracket.
    remove the square brackets around [I0,I] since you are treating I0 and I as variables in your code

    CB
    Follow Math Help Forum on Facebook and Google+

  9. #9
    Newbie
    Joined
    Oct 2008
    Posts
    24
    Okay, i assume i have to change the if thesis to 5 input arguments.


    Code:
    function [t,U]=minPrim(f,I0,I,U0,N)
    
    NrIn=nargin;
    if NrIn==5  
    
    elseif NrIn==5
    The new error message is:

    ??? Error using ==> functionname
    Too many input arguments.

    Error in ==> minPrim at 17
    U(ik)=U(ik-1)+k*f(t(ik-1),U(ik-1));



    Code:
    function [y]=functionname(x)
    
    
    y=x^2;


    It is strange that the expression inside the foor loop is incorrect when i try to run the program. Because in the file there is no indication that something is wrong (by showing red marks)
    Follow Math Help Forum on Facebook and Google+

  10. #10
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by techmath View Post
    Okay, i assume i have to change the if thesis to 5 input arguments.


    Code:
    function [t,U]=minPrim(f,I0,I,U0,N)
    
    NrIn=nargin;
    if NrIn==5  
    
    elseif NrIn==5
    The new error message is:

    ??? Error using ==> functionname
    Too many input arguments.

    Error in ==> minPrim at 17
    U(ik)=U(ik-1)+k*f(t(ik-1),U(ik-1));



    Code:
    function [y]=functionname(x)
    
    
    y=x^2;


    It is strange that the expression inside the foor loop is incorrect when i try to run the program. Because in the file there is no indication that something is wrong (by showing red marks)
    You do not need the time variable in the function call as the function is defined without it.

    CB
    Follow Math Help Forum on Facebook and Google+

  11. #11
    Newbie
    Joined
    Aug 2009
    Posts
    21

    Re: Matlab task

    Quote Originally Posted by techmath View Post
    Need to finish a program that approximates a first order differential equation with eulers method.


    Code:
    function [t,U]=minPrim(?,?,?,?)
    
    NrIn=nargin;
    if NrIn==?  
    
    elseif NrIn=?
      N=100;
    else
      error(’Wrong number of input arguments!’)
    end
    U=zeros(1,N+1);
    
    k=? %increment
    t=? %Vector with all t:s
    U(1)=U0;
    for k=2:?
      U(?)=U(?)+k*f(?);
    end
    4 input arguments

    f, function
    Vector with rand values (2 of them, this is the intervall)
    Initial value U0
    Number if intervalls N.


    This should solve for example:

    u'=x^2

    u0=3

    {x:0,2}
    There is a very good book written by Timothy Sauer called "Numerical Analysis" that you should take a look at. Here is the companion website:
    Numerical Analysis

    This might not be exactly what you are after, but it will give you a good platform. If you can get this book from your library I would recommend it as it will help you understand the method. Here is some code within the book (available from the link above):

    Code:
    %Program 6.1 Euler's Method for Solving Initial Value Problems
    %Use with ydot.m to evaluate rhs of differential equation
    % Input: interval [a,b], initial value y0, step size h
    % Output: time steps t, solution y
    % Example usage: y=euler([0 1],1,0.1);
    function [t,y]=euler(int,y0,h)
    t(1)=int(1); y(1)=y0;
    n=round((int(2)-int(1))/h);
    for i=1:n
      t(i+1)=t(i)+h;
      y(i+1)=eulerstep(t(i),y(i),h);
    end
    plot(t,y)
    
    function y=eulerstep(t,y,h)
    %one step of Euler's Method
    %Input: current time t, current value y,  stepsize h
    %Output: approximate solution value at time t+h
    y=y+h*ydot(t,y);
    
    function z=ydot(t,y)
    z = t*y + t^3;
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. IQ-task with colours
    Posted in the Math Puzzles Forum
    Replies: 7
    Last Post: July 30th 2012, 02:05 AM
  2. plane task
    Posted in the Statistics Forum
    Replies: 1
    Last Post: June 5th 2011, 10:16 PM
  3. Can somebody complete these task?
    Posted in the Algebra Forum
    Replies: 5
    Last Post: December 8th 2010, 12:34 PM
  4. Can somebody complete these task?
    Posted in the Algebra Forum
    Replies: 4
    Last Post: December 4th 2010, 01:18 PM
  5. hard task
    Posted in the Math Topics Forum
    Replies: 4
    Last Post: September 26th 2005, 08:50 AM

Search Tags


/mathhelpforum @mathhelpforum