Help plotting piecewise function?

13 views (last 30 days)
Aye
Aye on 26 Jul 2020
Answered: KSSV on 26 Jul 2020
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 Comments
Aye
Aye on 26 Jul 2020
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 on 26 Jul 2020
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.

Sign in to comment.

Answers (1)

KSSV
KSSV on 26 Jul 2020
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)

Products

Community Treasure Hunt

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

Start Hunting!