Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Math Help - m file failure?

  1. #1
    Junior Member
    Joined
    Apr 2010
    Posts
    59

    m file failure?

    Hey:

    Iv written this m-file, basically i want it to have a function, annd to plot it on a graph, then run another m-file within it to show the graph with small limits using its derivative, but when I run it, I don't get any results, i just get this:

    f =

    []

    Heres the m file:

    function [ y ] = f( x )
    % calculate f(x)=exp(x)
    y=exp(x);
    plot(x,f(x));
    function [ y ] = fd( x )
    % calculates fd(x)=exp(x)
    y=exp(x);
    plot(x,fd(x),'k')
    pause
    plot(x,(f(x+0.1)-f(x))/0.1,'r')
    pause
    plot(x,(f(x+0.01)-f(x))/0.01,'g')
    pause
    plot(x,(f(x+0.001)-f(x))/0.001,'b')
    pause
    plot(x,(f(x+0.0001)-f(x))/0.0001,'m')
    end
    end

    cheerz
    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 ramdrop View Post
    Hey:

    Iv written this m-file, basically i want it to have a function, annd to plot it on a graph, then run another m-file within it to show the graph with small limits using its derivative, but when I run it, I don't get any results, i just get this:

    f =

    []

    Heres the m file:

    function [ y ] = f( x )
    % calculate f(x)=exp(x)
    y=exp(x);
    plot(x,f(x));
    function [ y ] = fd( x )
    % calculates fd(x)=exp(x)
    y=exp(x);
    plot(x,fd(x),'k')
    pause
    plot(x,(f(x+0.1)-f(x))/0.1,'r')
    pause
    plot(x,(f(x+0.01)-f(x))/0.01,'g')
    pause
    plot(x,(f(x+0.001)-f(x))/0.001,'b')
    pause
    plot(x,(f(x+0.0001)-f(x))/0.0001,'m')
    end
    end

    cheerz
    This is a function definition file the first function in it is executed by invoking it, the second is only executed if invoked from the first (and it is not).

    1. Please post the calling statement (what you type on at the command prompt or have in the script file)

    2. Consider executing in the debugger.

    3. Look at all the red or amber marks to the right in the editor, each of these indicates a syntactical mistake or a warning that the code may not be doing what you think, so check them.

    4. You appear to be calling the top level function recursively, I doubt you intend this.

    CB
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Junior Member
    Joined
    Apr 2010
    Posts
    59
    Quote Originally Posted by CaptainBlack View Post
    This is a function definition file the first function in it is executed by invoking it, the second is only executed if invoked from the first (and it is not).

    1. Please post the calling statement (what you type on at the command prompt or have in the script file)

    2. Consider executing in the debugger.

    3. Look at all the red or amber marks to the right in the editor, each of these indicates a syntactical mistake or a warning that the code may not be doing what you think, so check them.

    4. You appear to be calling the top level function recursively, I doubt you intend this.

    CB
    Basically (I think this is rigHT), What I want it to do is simple. To have a function defined, and then to (through a series of commands) run an m-file, so I think its called a Script m file or something like that.

    To answer your questions:
    1. I don't type anything in the command window, I just right click on it, and run it - that is obviously wrong though.

    2. Not sure what you mean by this, but I will have a look.

    3. There are no marks in the "m file maker" its a green square, so nothing is wrong. Well something obviously is.

    4. Again, not sure what you mean by this, but Il take a second look at it.
    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 ramdrop View Post
    Basically (I think this is rigHT), What I want it to do is simple. To have a function defined, and then to (through a series of commands) run an m-file, so I think its called a Script m file or something like that.

    To answer your questions:
    1. I don't type anything in the command window, I just right click on it, and run it - that is obviously wrong though.
    How does it find its input data?

    2. Not sure what you mean by this, but I will have a look.

    3. There are no marks in the "m file maker" its a green square, so nothing is wrong. Well something obviously is.
    It is in the Matlab editor (answer for both of the above)

    4. Again, not sure what you mean by this, but Il take a second look at it.
    This is a function file not a script, only the top function is visible out side the file.

    CB
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Junior Member
    Joined
    Apr 2010
    Posts
    59
    Well, Iv had a major play with it at the moment, im trying to run the file first to see what happens. Except it just doesn't work,

    I get in the command window:
    ??? Input argument "x" is undefined.
    Error in ==> f at 3
    y=x^2;
    This clearly indicates to me that the variable, x is undefined and it will not work unless I define it.
    Im unsure as to how to define it, I know of one command, "syms 'x'" but that doesn't seem to work,

    Any ideas?
    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 ramdrop View Post
    Well, Iv had a major play with it at the moment, im trying to run the file first to see what happens. Except it just doesn't work,

    I get in the command window:


    This clearly indicates to me that the variable, x is undefined and it will not work unless I define it.
    Im unsure as to how to define it, I know of one command, "syms 'x'" but that doesn't seem to work,

    Any ideas?

    Yes, but I have already posted them, have you read them and acted on them?

    Please repost the content of the .m file its name and the data you are trying to pass to it and an explanation of how you are trying to invoke this function.

    Also post a clear statement of what you think you are trying to do (the exact wording of the problem as set)

    CB
    Last edited by CaptainBlack; April 23rd 2010 at 09:05 AM.
    Follow Math Help Forum on Facebook and Google+

  7. #7
    Junior Member
    Joined
    Apr 2010
    Posts
    59
    function [ y ] = f( x )
    % calculate f(x)=exp(x)
    y=exp(x);
    plot(x,f(x));
    function [ y ] = fd( x )
    % calculates fd(x)=exp(x)
    y=exp(x);
    plot(x,fd(x),'k')
    pause
    plot(x,(f(x+0.1)-f(x))/0.1,'r')
    pause
    plot(x,(f(x+0.01)-f(x))/0.01,'g')
    pause
    plot(x,(f(x+0.001)-f(x))/0.001,'b')
    pause
    plot(x,(f(x+0.0001)-f(x))/0.0001,'m')
    end
    end
    That is the m file, what I want it to do, is for values of x, plot the function e^x and show this on a graph.

    Then I want it to (after I input some command I think) probably the actual plot line in the file, to plot the derivative of e^x which is obviously the same, but with the small values towards the end of the m-file. We know the actual derivative, and its been plotted on a graph, the other "plots" get closer to the correct derivative. Thats what I want it to do
    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 ramdrop View Post
    That is the m file, what I want it to do, is for values of x, plot the function e^x and show this on a graph.

    Then I want it to (after I input some command I think) probably the actual plot line in the file, to plot the derivative of e^x which is obviously the same, but with the small values towards the end of the m-file. We know the actual derivative, and its been plotted on a graph, the other "plots" get closer to the correct derivative. Thats what I want it to do
    That is an m-file what happens when you run it?

    Also what about the other questions I asked? Most importantly how are you calling this and what argument are you passing to it?

    Also I have already pointed out the you are calling F recursively and you probably do not want to do that, do you want to do that? Have you done anything to fix that problem?

    CB
    Follow Math Help Forum on Facebook and Google+

  9. #9
    Junior Member
    Joined
    Apr 2010
    Posts
    59
    It comes up with the error when I run it:
    ??? Input argument "x" is undefined.
    Error in ==> f at 3
    y=x^2
    I have tried changing the functions, 1 being f and the other being g.

    The file itself is called "f.m"

    Im trying to write something in the command window, like f(5) or something and it comes with errors, I just need it to run basically
    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 ramdrop View Post
    It comes up with the error when I run it:


    I have tried changing the functions, 1 being f and the other being g.

    The file itself is called "f.m"

    Im trying to write something in the command window, like f(5) or something and it comes with errors, I just need it to run basically
    f requires a vector of points as input something like f([1,2,3,4,5]).

    CB
    Follow Math Help Forum on Facebook and Google+

  11. #11
    Junior Member
    Joined
    Apr 2010
    Posts
    59
    I want it for all values of x.

    Okay, so to write it, I would write in the m-file on the second line:

    f = [-inf,inf] ?
    Follow Math Help Forum on Facebook and Google+

  12. #12
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by ramdrop View Post
    I want it for all values of x.

    Okay, so to write it, I would write in the m-file on the second line:

    f = [-inf,inf] ?
    No you can't do such a plot! You are also not passing it a range to plot but a set of points.

    It seems to me that your level of understanding of what you want and what Matlab does is poor. So if we are to make any progress with this you will first have to explain why you are doing what you are trying to do.

    CB
    Follow Math Help Forum on Facebook and Google+

  13. #13
    Junior Member
    Joined
    Apr 2010
    Posts
    59
    My MATLAB knowledge is indeed poor because the lectures we have been given are just a sheet, the lecturer is never around to ask questions, usually these "sheets" are not really informative, they just work through examples, we're given them and then the lecturer wanders off for the majority of the class.

    Im trying to learn MATLAB by myself because next year, I have a lot of MATLAB to do..

    Okay, so this is EXACTLY what I want the m-file to do.

    I want the m-file (for any function, x, exp(x)) defined as f, to be plotted on a graph, probably the best would be a 10 by 10. This is represented by:
    function [ y ] = f( x )
    % calculate f(x)=exp(x)
    y=exp(x);
    plot(x,f(x));
    Then, I want it to differentiate the function f, and plot that graph. Then it "approximates" the derivative for values of x = 0.01, 0.001, 0.0001. This causes the "curve/line" of the graph to change and become more accurate.

    So in basic terms, the smaller the value of x, the more accurate the derivative plot is. This is represented by the latter part of the m-file.
    function [ y ] = fd( x )
    % calculates fd(x)=exp(x)
    y=exp(x);
    plot(x,fd(x),'k')
    pause
    plot(x,(f(x+0.1)-f(x))/0.1,'r')
    pause
    plot(x,(f(x+0.01)-f(x))/0.01,'g')
    pause
    plot(x,(f(x+0.001)-f(x))/0.001,'b')
    pause
    plot(x,(f(x+0.0001)-f(x))/0.0001,'m')
    end
    en
    Follow Math Help Forum on Facebook and Google+

  14. #14
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by ramdrop View Post
    My MATLAB knowledge is indeed poor because the lectures we have been given are just a sheet, the lecturer is never around to ask questions, usually these "sheets" are not really informative, they just work through examples, we're given them and then the lecturer wanders off for the majority of the class.

    Im trying to learn MATLAB by myself because next year, I have a lot of MATLAB to do..

    Okay, so this is EXACTLY what I want the m-file to do.

    I want the m-file (for any function, x, exp(x)) defined as f, to be plotted on a graph, probably the best would be a 10 by 10. This is represented by:


    Then, I want it to differentiate the function f, and plot that graph. Then it "approximates" the derivative for values of x = 0.01, 0.001, 0.0001. This causes the "curve/line" of the graph to change and become more accurate.

    So in basic terms, the smaller the value of x, the more accurate the derivative plot is. This is represented by the latter part of the m-file.
    Lets start at the begining:

    [code]
    function [ y ] = f( x )
    % calculate f(x)=exp(x)
    y=exp(x);
    plot(x,f(x));
    [\code]

    1. I have told you before that you have a recursive call to f in this function definition that you almost certainly don't intend.

    Create a file f.m which contains:

    Code:
    function [y]=f(x)
       y=exp(x);
    Now call this from the console with:

    Code:
    >>x=[-10:0.5:10]
    >>y=f(x)
    >>plot(x,y);
    Now report what happens.


    CB
    Follow Math Help Forum on Facebook and Google+

  15. #15
    Junior Member
    Joined
    Apr 2010
    Posts
    59
    I got a very nice plot, thanks, so I guess I do the same for the next part of the file?
    Follow Math Help Forum on Facebook and Google+

Page 1 of 2 12 LastLast

Similar Math Help Forum Discussions

  1. failure time
    Posted in the Advanced Statistics Forum
    Replies: 3
    Last Post: June 11th 2010, 04:23 AM
  2. Probabilty of failure
    Posted in the Statistics Forum
    Replies: 1
    Last Post: February 23rd 2010, 06:57 PM
  3. Assume that the time of failure..
    Posted in the Advanced Statistics Forum
    Replies: 6
    Last Post: January 1st 2010, 10:23 PM
  4. Mat lab - Referencing M-file in another M-file
    Posted in the Math Software Forum
    Replies: 5
    Last Post: December 16th 2009, 03:53 AM
  5. Variation of the Failure Rate
    Posted in the Advanced Statistics Forum
    Replies: 0
    Last Post: November 1st 2009, 04:43 AM

Search Tags


/mathhelpforum @mathhelpforum