for loop and plotting 3 functions

4 vues (au cours des 30 derniers jours)
Danah Alotaibi
Danah Alotaibi le 31 Jan 2021
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

Réponses (1)

Mathieu NOE
Mathieu NOE le 1 Fév 2021
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

Community Treasure Hunt

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

Start Hunting!

Translated by