Finding the last value of a difference equation

Hello!
So I've been assigned to solve this equation:
where A is a positive nymber and 0<=n<= N-1, while also N=30
Furthermore I have an initial value of y(-1)=3. I'm looking to find the last value of this equation (so for n=29).
Last, I want my function to be called back by using this code:
result = my_matlab_function(A,N);
disp(['A= ' num2str(A) 'Result=' num2str(result)])
This is what I've managed to write so far, yet MATLAB won't accept it:
function res = my_matlab_function(A,n)
assume(A,'positive');
n:0:1:29;
y(-1)=3;
y(n)=(1/2)*(y(n-1)+(A^2)/(y(n-1)));
result = my_matlab_function(A,N);
disp(['A= ' num2str(A) 'Result=' num2str(result)])
end

2 Comments

Note: Matlab allows the positive indices only, Y(1), Y(2) etc
Ok, but i want for the first value of n (meaning n=0), and given that y(-1)=3 to begin the calculations. So I've got to declare it somewhere!

Sign in to comment.

Answers (2)

Use:
y(end) = -3

5 Comments

I want as an initial value y(-1)=3 not y(-1)=-3
You actually want this:
function my_matlab_function(A)
if A < 0
% you don't really need this. You use A^2 later, it doesn't really
% matter A < 0 or not
disp('A must be positive');
return;
end
% initialize the first one in the list
y(1) = 3;
% you want apply the expression from n = -1 to n = 29
% but matlab has to start with i = 0. So it is i from 1 to 31
N = 31;
for i=2:N
% each y(i) depends on y(i-1)
y(i)=0.5*(y(i-1)+((A^2)/y(i-1)));
end
% plot it if you want to see how it goes through different n
% plot(1:N, y);
disp(['A= ' num2str(A) '; Result=' num2str(y(N))])
end
It returns:
Not enough input arguments.
Error in my_matlab_function (line 2)
if A < 0
And using Matlab Grader to solve my function, it returns:
Unrecognized function or variable 'A'.
You need to call this function with an A value.
  1. Save the function as my_matlab_function.m. It is a function that takes an input argument A.
function res = my_matlab_function(A)
if A < 0
% you don't really need this. You use A^2 later, it doesn't really
% matter A < 0 or not
disp('A must be positive');
return;
end
% initialize the first one in the list
y(1) = 3;
% you want apply the expression from n = -1 to n = 29
% but matlab has to start with i = 0. So it is i from 1 to 31
N = 31;
for i=2:N
% each y(i) depends on y(i-1)
y(i)=0.5*(y(i-1)+((A^2)/y(i-1)));
end
% plot it if you want to see how it goes through different n
% plot(1:N, y);
disp(['A= ' num2str(A) '; Result=' num2str(y(N))]);
res = y(N);
end
  1. From command line, call this function with an A value. Something like:
my_matlab_function(100)

Sign in to comment.

Then iterate 2-times more starting with n=1 instead of n=-1.
function res = my_matlab_function(A,N)
y = zeros(N+1,1)
y(1) = 3;
for i = 1:N
y(i+1) = 0.5*(y(i) + A^2/y(i))
end
res = y(end)
end

6 Comments

It returns:"Unrecognized function or variable 'A'."
And did you call my_matlab_function as
function main
A = 4;
N = 10;
res = my_matlab_function(A,N)
end
function res = my_matlab_function(A,N)
y = zeros(N+1,1);
y(1) = 3;
for i = 1:N
y(i+1) = 0.5*(y(i) + A^2/y(i));
end
res = y(end);
end
?
I'm looking to find the value of A and the result of the equation for n=29, given that I have an initial value of y(-1)=3. I can't declare them and use them as I wish
Torsten
Torsten on 24 Jan 2022
Edited: Torsten on 24 Jan 2022
You can't "find" A because you have no degree of freedom in the difference equation.
Maybe you want to prescribe the last value of y (say yend) after 30 iterations and determine A such that y(end) = yend ?
If you want the expression of y(30) dependent on A, you must use symbolic computations.
I've managed to create a new code, but I don't know how to implement that variable A is a positive variable
function res = my_matlab_function(A,N)
%ορισμός της αρχικής συνθήκης y(-1)=3
y_1=3;
assume (A,'positive');
% υπολογισμός της πρώτης τιμής χωρίς την χρήση του βρόχου
y(0)=0.5*(y_1+((A^2)/y_1));
%υπολογισμός των υπόλοιπων τιμών
for i=1:29
y(i)=0.5*(y(i-1)+((A^2)/y(i-1)));
result = my_matlab_function(A,N);
disp(['A= ' num2str(A) 'Result=' num2str(result)])
end
Well, maybe I was not clear in my answer: A has to be prescribed, it can't be deduced.
At least if you don't modify your question somehow.
And again: Arrays in Matlab start with index 1. 0 gives an error message.

Sign in to comment.

Products

Release

R2021b

Asked:

on 24 Jan 2022

Edited:

on 27 Jan 2022

Community Treasure Hunt

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

Start Hunting!