Not sure why my code result in different graphics
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Andrey Kondratov
el 30 de Oct. de 2019
Respondida: Bhaskar R
el 30 de Oct. de 2019
So I have done a piece of code that is supposed to make two equal graphics, just by using different methods to do so. However, end result doesnt seem to go as planned. Can anyone see what is wrong here? How can I fix it?
x = -pi : pi/500 : pi;
for i = -pi:pi/500:pi
if (x > 0)
y = sin(x).^2 + (1+x)./(1+exp(x));
else
y = (1+x.^2).^(1.0/3.0);
end
end
figure
plot(x,y);
grid on;
hold on;
y = ((1+x.^2).^(1.0/3.0)).*(x<=0) + (sin(x).^2 + (1+x)./(1+exp(x))).*(x>0);
plot(x,y);

0 comentarios
Respuesta aceptada
Bhaskar R
el 30 de Oct. de 2019
You have made for loop mistake and indexing in your code, correct one is
x = -pi : pi/500 : pi;
y =zeros(1, length(x)); % initialize legth of y
for i = 1:length(y) % you gave x sequence over instead of indexing sequence
if (x(i) > 0)
y(i) = sin(x(i)).^2 + (1+x(i))./(1+exp(x(i)));
else
y(i) = (1+x(i).^2).^(1.0/3.0);
end
end
figure
plot(x,y);
grid on;
hold on;
y = ((1+x.^2).^(1.0/3.0)).*(x<=0) + (sin(x).^2 + (1+x)./(1+exp(x))).*(x>0);
plot(x,y);
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Creating, Deleting, and Querying Graphics Objects 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!