Help plotting piecewise function?

조회 수: 14 (최근 30일)
Aye
Aye 2020년 7월 26일
답변: KSSV 2020년 7월 26일
I'm new to matlab and was wondering if someone could help me plot the following function as I don't really know where to start....
x is an integer value between 1 to 10
V_{x}N= if x is even, [(3/x)*(3/(x-2))...*(3/4)]*2*N^x
if x is odd, [(5/x)*(5/(x-2))...(5/3)]*pi
N is arbitrary but a value of N=1 is passed into the function in order to plot it
  댓글 수: 2
Aye
Aye 2020년 7월 26일
I'm thinking of creating an if else statement with the following structure
if (mod(x,2)==0)
eq1
else
eq2
end
but am having trouble figuring out how to code the equations
Mario Malic
Mario Malic 2020년 7월 26일
for x = 1 : 1 : 10
if mod(x,2) == 0
y(x) = [(3/x)*(3/(x-2))...*(3/4)]*2*N^x
else
y(x) = [(5/x)*(5/(x-2))...(5/3)]*pi
end
end
Vector y contains your functions from 1 to 10, then you'll have probably have to use for again to plot each on its interval if I understood correctly.

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

답변 (1개)

KSSV
KSSV 2020년 7월 26일
YOu can proceed like this:
x = 1:10 ;
y = zeros(size(x)) ;
N = 1 ;
% x is even
idx = 2:2:length(x) ;
y(idx) = [(3./x(idx)).*(3./(x(idx)-2))*(3/4)]*2.*N.^x(idx) ;
% x is odd
idx = 1:2:length(x) ;
y(idx) = [(5./x(idx)).*(5./(x(idx)-2))*(5/3)]*pi ;
plot(x,y)

제품

Community Treasure Hunt

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

Start Hunting!

Translated by