Borrar filtros
Borrar filtros

ive tried doing it but i dont know why the plot is not right t=-2:0.1:2; if t<=-1 y=-5.*t-5; elseif t<0 y=t.^2+1; elseif t<1 y=pi.^t; elseif t>=1 y=pi+sin(pi

2 visualizaciones (últimos 30 días)

Respuesta aceptada

David Hill
David Hill el 30 de Dic. de 2021
t=-2:.1:2;
f=zeros(size(t));
f(t<=-1)=-5*(t(t<=-1))-5;
f(t>-1&t<0)=t(t>-1&t<0).^2+1;
f(t>=0&t<1)=pi.^(t(t>=0&t<1));
f(t>=1)=pi+sin(pi*t(t>=1));
plot(t,f);
  2 comentarios
Colty Day
Colty Day el 30 de Dic. de 2021
uhm...sorry is there other way on doing it... what if i use if elseif
Image Analyst
Image Analyst el 31 de Dic. de 2021
David we normally don't give full solutions to what is obviously his homework. Colty, I know you accepted this but beware of turning it in. I know for a fact that some professors use "plagiarism detectors" and you don't want to get caught turning in someone else's solution as your own.
It looks like you actually wanted a version using if/else and I gave you hints for that and you almost got it except that t on the right hand side needed to be t(k).

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 30 de Dic. de 2021
Hint to get you started
function f(t)
ft = zeros(1, length(t));
for k = 1 : length(t)
if t(k) <= -1
ft(k) =
elseif
end
end
plot(t, ft, 'b-')
  5 comentarios
Colty Day
Colty Day el 31 de Dic. de 2021
Editada: Walter Roberson el 31 de Dic. de 2021
t=-2:0.1:2;
yt = zeros(1, length(t));
for k =1: length(t)
if t(k)<=-1
yt(k)=-5.*t-5;
elseif t(k)<0
yt(k)=t.^2+1;
elseif t(k)<1
yt(k)=pi.^t;
elseif t(k)>=1
yt(k)=pi+sin(pi.*t);
end
end
plot(t,yt,'r-');
Image Analyst
Image Analyst el 31 de Dic. de 2021
@Colty Day, you almost got it, but you need to use t(k), not the entire t vector, on the right hand side of the equation. If it works for you can you at least "Vote" for this answer. 🙂
t=-2:0.1:2;
yt = zeros(1, length(t));
for k = 1: length(t)
if t(k) <= -1
yt(k) = -5.*t(k)-5;
elseif t(k) < 0
yt(k) = t(k).^2+1;
elseif t(k) < 1
yt(k) = pi.^t(k);
elseif t(k) >= 1
yt(k) = pi + sin(pi.*t(k));
end
end
plot(t,yt,'r-');
grid on;

Iniciar sesión para comentar.

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!

Translated by