How can I evaluate a function at a vector and plot the returned values?

Using this function code: function [y1,y2] = f(x1)
y1 = 6*sin(x1) + cos(2*x1)
y2 = 12*sin(2*x1) + cos(4*x1)
end
I need to evaluate it at multiple points using the vector: x1 = linspace(0, 2*pi, 10);
So, I entered this code on my main script:
x1 = linspace(0, 2*pi, 10);
ii = f(x1)
It returns the error: Subscript indices must either be real positive integers or logicals.
Can someone please tell me what I am doing wrong? I know this is basic, but I am new to Matlab and my class on it isn't very helpful.

 채택된 답변

Matt J
Matt J 2016년 10월 9일
First type
clear f
then re-run.

댓글 수: 3

Thanks a lot. I am wondering though why do I have to do that? Also, do I have to do that for every function I evaluate at a vector?
You had to do it because you had a variable named "f" already existing in the workspace. Avoid conflict between functions and variable names and there will be no need for this.
Oh ok I didn't realize. Thanks again for your help.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2016년 10월 9일

댓글:

2016년 10월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by