Vectorization and plotting user-defined function

14 visualizaciones (últimos 30 días)
John Ball
John Ball el 4 de Sept. de 2020
Comentada: Adam Danz el 5 de Sept. de 2020
I am new to MATLAB and am having trouble understanding how to plot userdefined functions.
I have defined a function f via
fun=@(t,r)(t.^2-t.^4).*exp(r.*t.^2);
fun1=@(t,r)exp(r.*t.^2);
f=@(r)integral(@(t)fun(t,r),0,1)./integral(@(t)fun1(t,r),0,1);
Now if I specify say
r=linspace(-10,10);
and type
x=f(r);
I get errors such as
Matrix dimensions must agree.
Error in @(t,r)(t.^2-t.^4).*exp(r.*t.^2)
whereas
x=sin(r);
is accepted. I seem to have been careful to use .* etc. What is the difference between my f and sin, and how do I plot a parametrized graph (f(r),g(r))?

Respuesta aceptada

Adam Danz
Adam Danz el 4 de Sept. de 2020
Editada: Adam Danz el 4 de Sept. de 2020
To evaluate the integrals at each value in r,
x = arrayfun(f,r);
  2 comentarios
John Ball
John Ball el 4 de Sept. de 2020
Many thanks Adam for the superfast response. But if I do
>> x=arrayfun(T,r);
>> y=arrayfun(g,r);
>> fplot(T,g, [-100 100])
I get a plot but still get vectorization errors
Warning: Function behaves unexpectedly on array inputs. To improve performance,
properly vectorize your function to return an output with the same size and shape as
the input arguments.
So it seems I am not understanding something.
Adam Danz
Adam Danz el 4 de Sept. de 2020
I don't get that error when I apply this solution to the function in you question (I doubt you get that error with those functions, either). So, something is different between T and g compared to f.
What line is producing the error? Could you provide a new minimal working example that reproduces the error?

Iniciar sesión para comentar.

Más respuestas (1)

Steven Lord
Steven Lord el 4 de Sept. de 2020
By default integral calls the integrand function with a vector of values at which the integrand should be evaluated. There's no guarantee that this vector will have a size compatible with the size of your r vector.
If you tell integral that your function is array-valued by passing the name-value pair 'ArrayValued', true then integral will call your integrand with a scalar and expect an array as output. Try running the "Vector-Valued Function" example on the documentation page for the integral function with and without the name-value pair and compare the results.
  2 comentarios
John Ball
John Ball el 5 de Sept. de 2020
Thanks very much for this. Combining your and Adam's answer works fine, and I think I even understand why. I accepted his answer, but I would like to have accepted both, but don't see how. In any case, this being my first use of this forum I found the quick responses so helpful.
Adam Danz
Adam Danz el 5 de Sept. de 2020
@John, voting (thumbs up icon) is another way to show support/appreciation. Glad to hear that you found the forum useful!

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by