How to plot graph with multiple values of x in a function
13 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Lewis
el 11 de Nov. de 2021
Comentada: the cyclist
el 11 de Nov. de 2021
This code should produce graphs for y=(x^3)-(4*(x^2))-x-4. My code is incorrect because it is raising the whole x matrix to powers in the plot() function rather than considering each element in the matrix. How do I solve this?
Thanks
count=0
for i=[3,5,7,10,20,100]
count=count+1;
x=linspace(-5,2,i)
subplot(2,3,count)
plot(x,(x^3)-(4*(x^2))-x-4,'-xr')
end
0 comentarios
Respuesta aceptada
the cyclist
el 11 de Nov. de 2021
Editada: the cyclist
el 11 de Nov. de 2021
You need to use elementwise operations:
count=0;
for i=[3,5,7,10,20,100]
count=count+1;
x=linspace(-5,2,i);
subplot(2,3,count)
plot(x,(x.^3)-(4*(x.^2))-x-4,'-xr')
end
0 comentarios
Más respuestas (2)
Sulaymon Eshkabilov
el 11 de Nov. de 2021
A small (but crucial) err that is elementwise operation needed, e.g.:
count=0;
for i=[3,5,7,10,20,100]
count=count+1;
x=linspace(-5,2,i);
subplot(2,3,count)
plot(x,(x.^3)-(4*(x.^2))-x-4,'-xr')
end
0 comentarios
Sulaymon Eshkabilov
el 11 de Nov. de 2021
You may also consider to display a legend showing the number of x values taken for calc and plot in every iteration:
count=0;
for i=[3,5,7,10,20,100]
count=count+1;
x=linspace(-5,2,i);
subplot(2,3,count)
plot(x,(x.^3)-(4*(x.^2))-x-4,'-xr')
legend(['N_x = ', num2str(i)], 'location', 'best')
end
1 comentario
the cyclist
el 11 de Nov. de 2021
I like the legend idea. In this particular case, I would use
'location','southeast'
rather than 'best'.
Ver también
Categorías
Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!