# I wanna implement this function, but it says there is an syntax error..

3 vues (au cours des 30 derniers jours)
JongMyung le 21 Mai 2023
Modifié(e) : John D'Errico le 21 Mai 2023
function y=myfunc(x)
y=(x<0).*(2*exp(x)+(0<=x&x<1).*2+(1<=x&x<3).*(x^2-4*x+5)+(3<=x).*(1+cos(x-3));
endfunction
##### 0 commentairesAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens

Connectez-vous pour commenter.

### Réponse acceptée

John D'Errico le 21 Mai 2023
Modifié(e) : John D'Errico le 21 Mai 2023
What langiuage are you using when you end a function with the statement endfunction? Not MATLAB.
As well, IF you had posted the error message, it would have said something about parentheses. So I fixed that.
Finally, it also yells at you, because the function was not properly vectorized. So you wrote this:
y=(x<0).*(2*exp(x)+(0<=x&x<1).*2+(1<=x&x<3).*(x^2-4*x+5)+(3<=x).*(1+cos(x-3));
and I changed it to this:
y=(x<0).*(2*exp(x))+(0<=x&x<1).*2+(1<=x&x<3).*(x.^2-4*x+5)+(3<=x).*(1+cos(x-3));
The differences are important.
fplot(@myfunc,[-5,5])
function y=myfunc(x)
y=(x<0).*(2*exp(x))+(0<=x&x<1).*2+(1<=x&x<3).*(x.^2-4*x+5)+(3<=x).*(1+cos(x-3));
end
##### 0 commentairesAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Configure and View Diagnostics dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by