# Functions in Matlab

Discussion in 'Mac Programming' started by Eraserhead, Feb 2, 2007.

Joined:
Nov 3, 2005
Location:
UK
#1
I have an assignment to find the taylor expansion of a number and I am having problems with functions in Matlab in evaluating a function for a given variable.

Code:
```function [taylorO] = taylor2(func,n)
taylorO=0;
for i=0:n
func=diff(func);
taylorO=taylorO+func(0)/factorial(i)*pow(x,i);
end```
I get this error:

123??? Subscript indices must either be real positive integers or logicals.

Error in ==> taylor2 at 9
taylorO=taylorO+func(0)/factorial(i)*pow(x,i);

2. ### Zeke macrumors 6502

Joined:
Oct 5, 2002
Location:
Greenville, SC
#2
Edit: sorry, reread the code. Was only looking for the (i) before. Not sure what's going on.

I still can't find the error but I seem to recall getting that message when I tried to reference an array with index 0.

Joined:
Nov 3, 2005
Location:
UK
#3
The error is where because I'm trying to find the value of func at x=0. (where func is 'exp(x)' or something similar...)

4. ### atszyman macrumors 68020

Joined:
Sep 16, 2003
Location:
The Dallas 'burbs
#4

Matlab indexes arrays starting at 1. There is no value associated with func(0), func, assuming it is an n place array goes from 1 to n, not 0 to n-1.

Joined:
Nov 3, 2005
Location:
UK
#5
Sorry I made some mistakes in the code , some of the other code is wrong, now when func(0) is replaced by 1 it works. The code is now:
Code:
```function [taylorO] = taylor2(func,n)
syms x;
n=n+1;
taylorO=0;
for i=0:n
func=diff(func);
taylorO=taylorO+func(0)/factorial(i)*power(x,i);
end
```
the error is now

??? Subscript indices must either be real positive integers or logicals.

Error in ==> taylor2 at 7
taylorO=taylorO+func(0)/factorial(i)*power(x,i);

6. ### Zeke macrumors 6502

Joined:
Oct 5, 2002
Location:
Greenville, SC
#6
But func is a function defined in Matlab (hence being able to diff it) and thus isn't an array subject to the 0 index (or at least it shouldn't be unless I'm misreading something).

Nevermind, I'm an idiot. That's the problem. I forgot that in Matlab functions are arrays.

Joined:
Nov 3, 2005
Location:
UK
#7
Let me rephrase the question, I not being very clear . How do you find the value of a function at a chosen point (in this case 0)?

Thanks for all your help so far!

8. ### Zeke macrumors 6502

Joined:
Oct 5, 2002
Location:
Greenville, SC
#8
So to fix this, define x = 0 to be some number in the middle of your array. So if you're evaluating your function 100 times around x = 0, put x = 0 at 50. X = -5:1:5; (or whatever)
Then define a variable A = func(X).
Next, diffA = diff(A).
Then because X = 0 is at index 6, your Taylor code will be referencing index 6.

Edit: I haven't used Matlab in a while so I probably messed up on the definition of the vector X. Basically, you're looking to get a column vector where X = [-5; -4, -3...5] (at least in my example). For a Taylor expansion you'll probably want the grid finer and the deviation from 0 less unless it diverges at 0.

9. ### atszyman macrumors 68020

Joined:
Sep 16, 2003
Location:
The Dallas 'burbs
#9
What I see here if I have this correct is you give the function Taylor an array 'func' and a number of itterations 'n'.

you then go and determine the differences between adjacent values in the func array, and add the first element of func over i! multiplied by x.

am I reading this code correctly?

Edit: No I'm not, you're trying to pass in a function 'func' and the diff should give the derivative of 'func' I don't think that this is happening correctly.

The error you are seeing is due to the fact that the code sees the variable func as an array passed into your matlab function from the code that calls your Talor2 function. Since Matlab indexes all arrays starting at '1' rather than '0' func(0) is undefined and results in the error you see.

Now what I think you are trying to do is have func be a function along the lines of func(x)= x^2 + x -1 or some such thing....

the easiest way to do this and make sure everything is working is create a file, like your TaylorO file,

Code:
```function [y] = func(x)

y=x^2 + x -1;
```
and then eliminate the func input from your Taylor2 function like this

Code:
```function [taylorO] = taylor2(n)
```
Now your code should work. I think the main problem is that the func is not passing as you think it should.

Edit : Don't ever forget the best Matlab debug tool there is.... eliminate the semicolons at the end of your lines of code to get the results to print to the console window.... Doing this you can verify any variables and see exactly where your code stops working.

Joined:
Nov 3, 2005
Location:
UK
#10
I found a simple answer to my own question

For a function f, with a variable x, you can find f(0) you use the following code:

Code:
`subs(f,x,0)`