0
votes

I so far have the following

    y = log(x);

% Ask user for input values for h and M 
% M denotes the number of steps of the algorithm. 

h       = input('Input value h: ');
M       = input('Input value M: ');

%Initialize an MxM matrix
D = zeros(M);

phi = (1/(2*h)) * (y(x+h) - y(x-h));
print(phi);

I obtain the error

Error using symengine (line 58) Index exceeds matrix dimensions.

Error in sym/subsref (line 696) B = mupadmex('symobj::subsref',A.s,inds{:});

Error in RE (line 12) phi = (1/(2*h)) * (y(x+h) - y(x-h));

First, I believe I should be getting an error message about x not being defined. Second, I have no idea what the matrix dimension error is about. Third, and most importantly, how can I declare the function phi so that it becomes what I wrote?

1

1 Answers

0
votes

First, I believe I should be getting an error message about x not being defined.

I'm guessing that x is defined, or you would get that error upon the line defining phi. To check whether x is defined, type "who" or "whos".

Second, I have no idea what the matrix dimension error is about.

This is most likely because y is a scalar, x + h is equal to some nonzero integer that is not 1, and you're trying to access y(x + h). For your own edification try setting y equal to a scalar (e.g. y = 5;) and seeing what errors are produced by indexing it in various legitimate and non-legitimate ways (e.g. y(1), y(0), y(3), y(-1), y(1.5)).

Third, and most importantly, how can I declare the function phi so that it becomes what I wrote?

Based on the context it looks like you want y to be defined as a function of x instead of a scalar. In other words:

y = @(x)log(x);
phi = (1/(2*h)) * (y(x+h) - y(x-h));

The code runs without error when you change the definitions to the above.

One other error you will run into: the print command is not what you're looking for - this prints a figure to a file. You're probably looking for:

disp(phi);