Make a function accept vector

조회 수: 2 (최근 30일)
Tim
Tim 2018년 3월 12일
답변: KL 2018년 3월 12일
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.
Thanks in advance!
  댓글 수: 1
Tim
Tim 2018년 3월 12일
ah i did it. its function y = myfunction(x)
y=x.*(exp(x.*(-0.7))).*sqrt((x.^2)*2+1)
end

댓글을 달려면 로그인하십시오.

답변 (1개)

KL
KL 2018년 3월 12일
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

카테고리

Help CenterFile Exchange에서 Time Series Events에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by