# Make a function accept vector

3 visualizzazioni (ultimi 30 giorni)
Tim il 12 Mar 2018
Risposto: KL il 12 Mar 2018
Hello, I am trying that this function below accepts vector inputs, but it doesnt want to work.
function y = myfunction(x)
y=x*(exp(x*(-0.7)))*sqrt(2*x^2+1)
end
I tried to replace the x* with x.*, but it wont work either. Maybe I am calling it wrong? myfunction(5) works, but myfunction([5 2]) does not.
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Tim il 12 Mar 2018
ah i did it. its function y = myfunction(x)
y=x.*(exp(x.*(-0.7))).*sqrt((x.^2)*2+1)
end

Accedi per commentare.

### Risposte (1)

KL il 12 Mar 2018
This is called the element-wise operation. When you write x^2, MATLAB tries to find x*x but when you write x.^2, it calculates square of every element.
>> x = 1:3;
>> x^2
Error using ^
Inputs must be a scalar and a square matrix.
To compute elementwise POWER, use POWER (.^) instead.
>> x.^2
ans =
1 4 9
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Time Series Events in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by