필터 지우기
필터 지우기

Plotting function with differenting constant

조회 수: 1 (최근 30일)
Efrat Hazan Elimelech
Efrat Hazan Elimelech 2022년 7월 19일
편집: Voss 2022년 7월 19일
Hi,
I'm pretty unexperienced with Matlab so pls bear with me:
I'm trying to plot the function
y=a/(x+1)-(a-x)^2+x/(x+a)
when a is a vector of
a=[1,2,3,50,300]
so I put it in the loop:
for i=1:length(a)
ai=a(i);
y{i}=@(x) (ai/(x+1)-(ai-x)^2+x/(x+ai));
fplot(y{i});
end
but I get just one graph (of ai=300) and tons of yellow warnings..
any comments?

채택된 답변

Voss
Voss 2022년 7월 19일
a=[1,2,3];%,50,300]; % using just the first 3 a values, for this demonstration
for i=1:length(a)
ai=a(i);
% y{i}=@(x) (ai/(x+1)-(ai-x)^2+x/(x+ai));
y{i}=@(x) (ai./(x+1)-(ai-x).^2+x./(x+ai)); % using element-wise operations (./ and .^) to vectorize
fplot(y{i});
hold on % using hold on to show multiple lines at once
end
  댓글 수: 2
Efrat Hazan Elimelech
Efrat Hazan Elimelech 2022년 7월 19일
thanks a lot!
Voss
Voss 2022년 7월 19일
편집: Voss 2022년 7월 19일
You're welcome!

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

추가 답변 (1개)

Torsten
Torsten 2022년 7월 19일
Include the command
hold on
after the line
fplot(y{i});
such that preceeding graphs are not overwritten.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by