program to display the curve of the function

조회 수: 1 (최근 30일)
adam ski
adam ski 2019년 4월 13일
답변: Akshat 2024년 9월 4일
program pour afficher la courbe de la fonction suivant
g=2t+2 y[0,2[
g=2 cos(4pi*t) t=[2,3]
for t=[0:3];
if t<2
g=2*t+2;
plot(g)
else t>=2
y=2*cos(4*pi*t);
plot(y)
end
end

답변 (1개)

Akshat
Akshat 2024년 9월 4일
Hi Adam,
I see you are trying to plot the "g" when "t" is lesser than 2, and "y" when "t" is greater than or equal to 2.
These are the points I think you are missing:
  1. In order to get a plot, you need two arrays, with the values containing at the corresponding time stamps.
  2. In the function definition above the code given by you, you haven't specified anything about "y". I am going to assume that the function is only "g" and it takes different functions at different time stamps.
  3. The time stamps that you are taking, are [0,1,2,3]. This is a very big time gap, and the curve will not be smooth, rather it will just have 4 points. Hence, I have changed the step size to 0.1, giving us 40 points to work with, making the curve a little more smooth. This way the points will be [0,0.1,0.2.....2.8,2.9,3].
Here is the code with changes that I suggested:
t = 0:0.1:3; % Use a finer step for smoother plot
g = zeros(size(t));
for i = 1:length(t)
if t(i) < 2
g(i) = 2*t(i) + 2;
else
g(i) = 2*cos(4*pi*t(i));
end
end
figure;
plot(t, g, 'LineWidth', 2);
xlabel('t');
ylabel('g(t)');
title('Piecewise Function Plot');
grid on;

카테고리

Help CenterFile Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by