The Student Room Group

matlab - function m files and area command

hi, i've made this function m-file but i don't know if i have done this correctly as i've always used script m-files. when i run it, it works and my graph comes up aswell... here are my commands..

function y = cos(x)
x = [0:0.1:20]
plot(-cos(x))
area(x,-cos(x))
title(['Plot of -cos(x)'])
xlabel(['x'])
ylabel(['y'])
end

i kept getting an error when i put y = -cos(x). i tired many things but it didnt work so I have left it as y = cos(x) but have used -cos(x) in lines below. Is this function m-file done correctly?

Also, i would like to ask what command lets me shade the area under the graph. i have used

fill(x,-cos(x),'r') this worked but gave me a diagonol line to show the area, if that makes sense, i also tried area(x,-cos(x)). this gave me the straight line but i can't change the colour of the shaded area. It comes out blue and would like to change it.

Can anybody help me, thankyou.
Reply 1
I'm quite horrible with matlab myself, but normally with a function like "function y = cos(x)" you have just said you have a function of a dependent variable y called "cos" of a variable (indep.) x. I would guess that Matlab is having trouble with you trying to name a function with a non-alphanumerical character to start the function name.

I think it'll work if you write
function y = someword(x)
y=-cos(x)

Then typing someword(pi) will compute -cos(pi)=1

Because cos is an inbuilt function (as far as I know - I haven't actually used it! We only do programming in Matlab, not really any graphing or similar)

I hope this is correct and makes sense...
Reply 2
nota bene
I'm quite horrible with matlab myself, but normally with a function like "function y = cos(x)" you have just said you have a function of a dependent variable y called "cos" of a variable (indep.) x. I would guess that Matlab is having trouble with you trying to name a function with a non-alphanumerical character to start the function name.

I think it'll work if you write
function y = someword(x)
y=-cos(x)

Then typing someword(pi) will compute -cos(pi)=1

Because cos is an inbuilt function (as far as I know - I haven't actually used it! We only do programming in Matlab, not really any graphing or similar)

I hope this is correct and makes sense...


yeah, it works when i substitute values of x into the y = equation but when i run the file i get an error message in the command window. in the function m file i am also trying to run the graph and my graph wont come up until i have solved the error.
Reply 3
Can you try figuring out where the error occurs, by splitting up the code? Or is the error message any helpful in saying what's wrong?

I don't have access to matlab on this computer so I can't fiddle with it myself.

I've never used the plot command; might it work better with "y" instead of "-cos(x)"? I (Then carrying this to the area command)


I suspect you could just write something like

x=[0:0.1:20];
y=-cos(x);
plot(x,y)
area(x,y)
title(['Plot of -cos(x)'])
xlabel(['x'])
ylabel(['y'])
Reply 4
nota bene
Can you try figuring out where the error occurs, by splitting up the code? Or is the error message any helpful in saying what's wrong?

I don't have access to matlab on this computer so I can't fiddle with it myself.

I've never used the plot command; might it work better with "y" instead of "-cos(x)"? I (Then carrying this to the area command)


I suspect you could just write something like

x=[0:0.1:20];
y=-cos(x);
plot(x,y)
area(x,y)
title(['Plot of -cos(x)'])
xlabel(['x'])
ylabel(['y'])


i found the problem. for some reason, i needed to put the x=[0:0.1:20] before the y=-cos(x). when i put it at the second line it gave me the error, but as long as the x= was above it it worked. im not sure why though. thankyou for your help.
Reply 5
"function y = cos(x)" implies that the following code is defining a function cos(x), where x is the input and y is the output. Are you sure you really need that line at all, because it looks to me like you just want to run a script, not define a function.

You're also trying to use x as an argument before you've assigned it.
Reply 6
sarah_vickers
for some reason, i needed to put the x=[0:0.1:20] before the y=-cos(x).


It's because you were trying to evaluate cos(x) when you hadn't actually assigned x yet. x=[0:0.1:20] simply defines a vector and y=-cos(x) computes another, which you can't do without having the input vector first.
Reply 7
Planto
"function y = cos(x)" implies that the following code is defining a function cos(x), where x is the input and y is the output. Are you sure you really need that line at all, because it looks to me like you just want to run a script, not define a function.
Nope, exactly. See my suggested code in the last post.
Reply 8
i see thankyou very much for your help.

Latest