- Vectorize the function: https://www.mathworks.com/help/matlab/matlab_prog/vectorization.html
- Use a loop/ARRAYFUN within the function so that it works over multiple input values.
- Call the function multiple times on scalar values, e.g. using a loop or ARRAYFUN:
Trying to graph a function I made but it seems to only take the first x value.
33 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Lucas
el 29 de Nov. de 2024 a las 6:34
Comentada: Lucas
el 29 de Nov. de 2024 a las 17:32
For one of my classes i was tasked with making a function that will solve for the area under a function, specifically the frensel function. I have created the function and it seems to work well, but when it comes to the second half of the assignment which happens to be graphing the change in the area from x= 0-3 next to the function sin(0.5*pi*(x)^2) I cant seem to get it to work correctly. I can get it to graph the sin function but when it tries to graph the function I made it only uses 0.
(Sorry if the format or anything looks weird this is my first time using any help forums)
Not looking for anyone to solve my problem 100% for me, Just hoping someone can point me in the right direction or tell me what I am doing wrong.
0 comentarios
Respuesta aceptada
Stephen23
el 29 de Nov. de 2024 a las 7:54
Editada: Stephen23
el 29 de Nov. de 2024 a las 7:55
Explanation
The basic problem is that you wrote your function assuming only a scalar input:
Fresnel(2)
However you did not write the function to accept a non-scalar input:
Fresnel(1:5)
The COLON operator simply uses the first element of any non-scalar input and disregards the rest. The warning is a prompt for you to investigate what your code is doing.
Solutions
You could do one of these:
x = 0:0.01:3;
y = arrayfun(@Fresnel,x);
y2=sin(0.5*pi.*(x).^2);
plot(x,y,'bx',x,y2)
3 comentarios
Más respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!