How to call a function with a vector?
26 次查看(过去 30 天)
显示 更早的评论
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 个评论
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
2019-3-6
编辑:Stephen23
2019-3-6
>> 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
2019-3-6
编辑:Kevin Phung
2019-3-6
if you are multiplying or dividing vectors, include the element-wise operator
.* %multiplication
./ %division
example documentation:
3 个评论
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)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!