for loop and plotting 3 functions

1 次查看(过去 30 天)
how to use for loop to plot these 3 functions, the increment for the axis is 0.1 as shown
x1=0:0.1:3; %first range
x2=3:0.1:6; %second range
x3=6:0.1:8; %third range
f1= @(x1) cos(x1); %first function
f2= @(x2) exp(-0.4*x2); %second function
f3= @(x3) 2*cos(x3); %third function
plot(x1,f1(x1),x2,f2(x2),x3,f3(x3))
grid
title('Function F(x)');
xlabel('ranges');
ylabel('Functions');
legend

回答(1 个)

Mathieu NOE
Mathieu NOE 2021-2-1
hello
mu suggestion below :
for ci = 1:3 % nb of functions to be displayed
if ci == 1
x=0:0.1:3; %first range
y = cos(x); %first function
elseif ci == 2
x=3:0.1:6; %second range
y = exp(-0.4*x); %second function
elseif ci == 3
x = 6:0.1:8; %third range
y = 2*cos(x); %third function
end
plot(x,y)
hold on
grid on
title('Function F(x)');
xlabel('ranges');
ylabel('Functions');
legend
end
hold off

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by