recur.m function

64 ビュー (過去 30 日間)
Anthony Walden
Anthony Walden 2022 年 6 月 19 日
コメント済み: Anthony Walden 2022 年 6 月 19 日
I need the recur.m file for my lab to be completed, I followed the link in my lab http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=2148 . I tried to save this 1 kb code as recur.m in my MATLAB folder, which I show to be there, it shows a file named 'recur' and is of type MATLAB code. I assume this is the file I need for the code to reference and operate properly. However when I try to reference the recur function I get an error message:
function y = recur(a,b,n,x,x0,y0);
Error: Function definition are not supported in this context. Functions can only be created as local or nested functions in code files.
Does anyone have any suggestions to help this file work properly so I can finish my lab, my online instructor is not being much help.

回答 (2 件)

Voss
Voss 2022 年 6 月 19 日
This syntax:
function y = recur(a,b,n,x,x0,y0);
defines the function recur. If you have already defined the function in an m-file, and now you only need to run it, you can do so like this:
result = recur(0,1,3,0.75,0.5,1.1);
which runs the function recur using those inputs (0,1,3, ... - you would choose your own inputs to use) and stores the output from recur in the variable result.
  1 件のコメント
Anthony Walden
Anthony Walden 2022 年 6 月 19 日
I guess I am wondering if I got the recur file saved into my MATLAB folder correctly. Surely this means I got the recur.m file saved into my MATLAB folder, did I not?

サインインしてコメントする。


Torsten
Torsten 2022 年 6 月 19 日
Works for me.
a = 1:10; %N=10
b = 1:5; %M=4
n = 11:25;
x = 11:25;
x0 = 3:6;
y0 = 1:10;
y = recur(a,b,n,x,x0,y0)
y = 1×15
-151 -21 146 184 56 -133 -173 -23 168 210 62 -1909 -692 3033 4289
function y = recur(a,b,n,x,x0,y0);
%
% y = recur(a,b,n,x,x0,y0)
% solves for y[n] from:
% y[n] + a1*y[n-1] + a2*y[n-2]... + an*y[n-N]
% = b0*x[n] + b1*x[n-1] + ... + bm*x[n-M]
%
% a, b, n, x, x0 and y0 are vectors
% a = [a1 a2 ... aN]
% b = [b0 b1 ... bM]
% n contains the time values for which the solution will be computed
% y0 contains the initial conditions for y, in order,
% i.e., y0 = [y[n0-N], y[n0-N+1], ...,y[n0-1]]
% where n0 represents the first element of n
% x0 contains the initial conditions on x, in order
% i.e., x0 = [x[n0-M],...,x[n0-1]]
% the output, y, has length(n)
%
N = length(a);
M = length(b)-1;
if length(y0) ~= N,
error('Lengths of a and y0 must match')
end
if length(x0) ~= M,
error('Length of x0 must match length of b-1')
end
y = [y0 zeros(1,length(n))];
x = [x0 x];
a1 = a(length(a):-1:1); % reverses the elements in a
b1 = b(length(b):-1:1);
for i=N+1:N+length(n),
y(i) = -a1*y(i-N:i-1)' + b1*x(i-N:i-N+M)';
end
y = y(N+1:N+length(n));
end
  1 件のコメント
Anthony Walden
Anthony Walden 2022 年 6 月 19 日
Thank you, I was trying to use the recur file as part of my code and did realize i just needed to referecne the file in my code. Thanks for the help!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeTime Series Objects についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by