Effacer les filtres
Effacer les filtres

When i run my code it says Unrecognized function or variable A.

2 vues (au cours des 30 derniers jours)
function res = my_matlab_function(A,N)
A=4;
N=30;
y(1)=1/2*(3+(A^2/3));
y(2)=1/2*(y(1)+(A^2/y(1)));
y(3)=1/2*(y(2)+(A^2/y(2)));
y(4)=1/2*(y(3)+(A^2/y(3)));
y(5)=1/2*(y(4)+(A^2/y(4)));
for n=6:(N-1)
y(n)=1/2*(y(n-1)+A^2/y(n-1));
end
res=y(end);
disp(['A= ' num2str(A) 'Result=' num2str(res)])
end
%when i call my function:
result = my_matlab_function(A,N)
disp(['A= ' num2str(A) 'Result=' num2str(result)])

Réponse acceptée

Davide Masiello
Davide Masiello le 4 Fév 2022
You need to define A and N before passing them to the function.
Try this:
A = 4;
N = 30;
result = my_matlab_function(4,30);
function res = my_matlab_function(A,N)
y(1)=1/2*(3+(A^2/3));
y(2)=1/2*(y(1)+(A^2/y(1)));
y(3)=1/2*(y(2)+(A^2/y(2)));
y(4)=1/2*(y(3)+(A^2/y(3)));
y(5)=1/2*(y(4)+(A^2/y(4)));
for n=6:(N-1)
y(n)=1/2*(y(n-1)+A^2/y(n-1));
end
res=y(end);
fprintf('A = %d\n',A)
fprintf('Result = %d\n',res)
end

Plus de réponses (1)

Enrico Gambini
Enrico Gambini le 4 Fév 2022
Define A=4 outside the function

Catégories

En savoir plus sur Genomics and Next Generation Sequencing dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by