Using embedded matlab funciton to find high accuracy derivative
Show older comments
Hi, I want to use embedded matlab function to take a high accuracy derivative of a sin wave. I have used sine wave block from simulink library, with 101 samples per period, 0 offset, 1 amplitude and sample time of 0.01. The solver is configured as fixed step solver (ode 4) with the same step size of 0.01. The simulation time is set as 1 sec.
The sine signal is named as B and here is the code I am using in the embedded funciton block. The formula for the derevative that I want to use uses central difference technique and is as follows.
B'(n))=(-B(n+2)+8B(n+1)-8B(n-1)+B(n-2))/(12.*step size); where, B is the input signal, n is the current time step I have successfuly tested the formula using command line and script and it is working, but when I use the same code in embedded matlab function, I get out of bounds error.
Here is the code that I am using:
function D = fcn(B) D=zeros (101,1); for n=3:99 D(n)=(-B(n+2)+8.*B(n+1)-8.*B(n-1)+B(n-2))./(12.*0.01); end This is the error message that I receive: Runtime error: Index into array out of range Attempted to access 5 element of data B. The valid index range is 1 to 1. I think it is due to the signal size problem, but I dont know how to fix it. Please help me, I will really appreciate you help! Thanks
Accepted Answer
More Answers (0)
Categories
Find more on Signal Attributes and Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!