How to call a function with a vector?

I need to modify the following code so that the function can be called with a vector for the value of b instead of just a single value. Any ideas?

댓글 수: 4

Adam
Adam 2019년 3월 6일
편집: Adam 2019년 3월 6일
Depends what f does, for a start.
To vectorize expressions to operate on element basis, use the "dot" operators...
f=@(x) cos(x.^2);
Whereever else you have similar expressions, must make same fixes.
See the section "Array vs. Matrix Operations" under the "Operators and Elementary Operations" section.
Stephen23
Stephen23 2019년 3월 6일
You need to understand the difference between array and matrix operations, otherwise your MATLAB code will produce nonsense and you won't know why:
Stephen23
Stephen23 2019년 3월 6일
편집: Stephen23 2019년 3월 6일
@Issie: you forgot to actually call the function. What you did:
>> result = (@(x) sin(x.^2), 0, 3, 50)
^^ Where is the function you want to call?
What you need:
>> result = fresnelS(@(x) sin(x.^2), 0, 3, 50)
^^^^^^^^ Your function !!!
PS: please do not post screenshots, posting actual text is much better (we already know what error message look like).

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

답변 (1개)

Kevin Phung
Kevin Phung 2019년 3월 6일
편집: Kevin Phung 2019년 3월 6일

1 개 추천

if you are multiplying or dividing vectors, include the element-wise operator
.* %multiplication
./ %division
example documentation:

댓글 수: 3

madhan ravi
madhan ravi 2019년 3월 7일
? where was vector multiplied or divided?
Adam
Adam 2019년 3월 7일
편집: Adam 2019년 3월 7일
h will be a vector if b is and h is multiplied and a division used to create it from b.
Kevin Phung
Kevin Phung 2019년 3월 7일
편집: Kevin Phung 2019년 3월 7일
Thanks Adam. To clarify, given b is a vector , y and h would become vectors in:
h = (b-a)./n;
y = f(a)+f(b); %where f is cos(x^2)

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

카테고리

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

질문:

2019년 3월 6일

편집:

2019년 3월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by