Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Eraserhead

macrumors G4
Original poster
Nov 3, 2005
10,434
12,250
UK
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);
 

Zeke

macrumors 6502a
Oct 5, 2002
507
1
Greenville, SC
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.
 

Eraserhead

macrumors G4
Original poster
Nov 3, 2005
10,434
12,250
UK
Edit: sorry, reread the code. Was only looking for the (i) before. Not sure what's going on.

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

atszyman

macrumors 68020
Sep 16, 2003
2,437
16
The Dallas 'burbs
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);


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.
 

Eraserhead

macrumors G4
Original poster
Nov 3, 2005
10,434
12,250
UK
Sorry I made some mistakes in the code :eek:, 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);
 

Zeke

macrumors 6502a
Oct 5, 2002
507
1
Greenville, SC
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.

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.
 

Eraserhead

macrumors G4
Original poster
Nov 3, 2005
10,434
12,250
UK
Nevermind, I'm an idiot. That's the problem. I forgot that in Matlab functions are arrays.

Let me rephrase the question, I not being very clear :eek:. How do you find the value of a function at a chosen point (in this case 0)?

Thanks for all your help so far!
 

Zeke

macrumors 6502a
Oct 5, 2002
507
1
Greenville, SC
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.
 

atszyman

macrumors 68020
Sep 16, 2003
2,437
16
The Dallas 'burbs
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);

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.
 

Eraserhead

macrumors G4
Original poster
Nov 3, 2005
10,434
12,250
UK
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)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.