How to write code in MATLAB?

조회 수: 1 (최근 30일)
Rezky Putra Barens
Rezky Putra Barens 2021년 2월 19일
답변: Paresh yeole 2021년 2월 19일
How to write code in this below in MATLAB using if, elseif?
x = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
f(x) = 2x^2+x^2-1, x<8
f(x) = x^2-x+1, x>=2 f(x) = 0, 2<=x<7
  댓글 수: 1
KALYAN ACHARJYA
KALYAN ACHARJYA 2021년 2월 19일
편집: KALYAN ACHARJYA 2021년 2월 19일
Homewok Question: Please refer MATLAB Onramp

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

답변 (2개)

Diaa
Diaa 2021년 2월 19일
Your code in the question is not clear to me, nevertheless, my code below can be edited to fulfil your needs.
x = 1:10; % array of independent variable
result = zeros(size(x)); % create an array of the output values to be edited
for n = x % loop over the x values
% change the conditions below to satisfy your needs
if n <= 2
result(n) = n^2 - n + 1;
elseif n > 8
result(n) = 2*n^2 + n^2 - 1;
else
result(n) = 0;
end
end
% variable "result" now has the corresponding output values

Paresh yeole
Paresh yeole 2021년 2월 19일
for x=1:10
if x<=2
f(x) = x*x - x + 1;
elseif x<=7 && x>2
f(x) = 0;
else
f(x) = 2*x*x + x*x-1;
end
end
The limits are not clear in the question. But you can adjust them in the if else accordingly.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by