How to call a function with a vector?

27 Ansichten (letzte 30 Tage)
Issie
Issie am 6 Mär. 2019
Bearbeitet: Issie am 7 Mär. 2019
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 Kommentare
Stephen23
Stephen23 am 6 Mär. 2019
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 am 6 Mär. 2019
Bearbeitet: Stephen23 am 6 Mär. 2019
@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).

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Kevin Phung
Kevin Phung am 6 Mär. 2019
Bearbeitet: Kevin Phung am 6 Mär. 2019
if you are multiplying or dividing vectors, include the element-wise operator
.* %multiplication
./ %division
example documentation:
  3 Kommentare
Adam
Adam am 7 Mär. 2019
Bearbeitet: Adam am 7 Mär. 2019
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 am 7 Mär. 2019
Bearbeitet: Kevin Phung am 7 Mär. 2019
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)

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by