# Math Help - m file failure?

1. ## 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

2. Originally Posted by ramdrop
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

3. Originally Posted by CaptainBlack
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.

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.

4. Originally Posted by ramdrop
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.

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

5. 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?

6. Originally Posted by ramdrop
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

7. 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

8. Originally Posted by ramdrop
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

9. 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

10. Originally Posted by ramdrop
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

11. 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] ?

12. Originally Posted by ramdrop
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

13. 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

14. Originally Posted by ramdrop
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

15. I got a very nice plot, thanks, so I guess I do the same for the next part of the file?

Page 1 of 2 12 Last