How can I plot two functions in the same graph?

I have to show that the functions f(x)=x/10 and g(x)=cos(x) intersect at least twice the range [-3,3]. Can anybody help me?

댓글 수: 1

Hi Íris!
In my view, there are two ways of tackling this problem. Firstly, doing this;
%------------------------------------------------------------------------------------------------------------------------------------------------------------
x = -3:0.1:3;
y1 = x/10;
y2 = cos(x);
%Firstly, just plot one single function;
figure(1)
plot(x,y1,"r-","Linewidth",2);
hold on
%Then, plot the rest of the functions;
plot(x,y2,"b-","Linewidth",2);
I think this way of plotting, despite having more lines of code, works too!
I hope this could help you!
R.G.

댓글을 달려면 로그인하십시오.

 채택된 답변

Mischa Kim
Mischa Kim 2014년 9월 17일

15 개 추천

Iris, use
x = -3:0.1:3;
f = x./10;
g = cos(x);
plot(x,f,x,g)

댓글 수: 4

Íris
Íris 2014년 9월 17일
Thanks that helped!
Marios Levi
Marios Levi 2017년 9월 21일
편집: Marios Levi 2017년 9월 21일
a smile sent your way :)
how can someone plot two graphs with different baseline on single graph plot?
rockstar49
rockstar49 2020년 10월 25일
Using this way to plot multiple functions, how can you use the legend on function to differenitate between each line?

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Aayushii Goswami
Aayushii Goswami 2017년 10월 21일

2 개 추천

I have to sketch the graph of y^2=8x and x^2=8y on the same axis system and shade the bounded area.can anyone pls help me?
Realeboha Ramoeletsi
Realeboha Ramoeletsi 2021년 7월 9일

1 개 추천

t=linspace(0.2,50,100);
alpha=0.5;
c1=2;
r=0.2;
sigma=0.25;
Nt=1600;
Ns=160;
Smax=20;
Smin=0;
T=1.;
E=10;
dt=(T/Nt);
ds=(Smax-Smin)/Ns;
V(1:Ns+1,1:Nt+1)=0.0;
S=Smin+(0:Ns)*ds;
tau=(0:Nt)*dt;
V(1:Ns+1,1)=max(S-E,0);
V(1,1:Nt+1)=0;
V(Ns+1,1:Nt+1)=Smax-E*exp(-r*tau);
y1=C(t,alpha,c1);
figure(1)
plot (S,V(:,1),'r-',S,V(:,round(Nt/2)),'g-',S,V(:,Nt+1),'b-');
plot(t,y1,'-r')
xlabel('S')
ylabel('V(S,tau)')
for j=1:Nt
for n=2:Ns
V(n,j+1)=0.5*dt*(sigma*sigma*n*n-r*n)*V(n-1,j)+(1-dt*(sigma*sigma*n*n+r))*V(n,j)+0.5*dt*(sigma*sigma*n*n+r*n)*V(n+1,j);
end
end
function y=C(t,alpha,c1)
if ~all(t>0)
error(" t values must be positive")
end
if alpha<0 || alpha>1
error("alpha must be in the range (0,1) excluding both ends")
end
y=c1*t.^(alpha-1)./gamma(alpha);
end
i have two functions y and V and i did plot them separately one on its plot, so i am having a problem on ploting the same fuctions in one figure, can someone help me please. thank you.
i have attached .m file
Nilankan Pal
Nilankan Pal 2021년 5월 13일

0 개 추천

x = -3:0.1:3;
f = x./10;
g = cos(x);
plot(x,f,x,g)

질문:

2014년 9월 17일

댓글:

2022년 2월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by