Help me plot this easy function

Status
Not open for further replies.

markwilest

Newbie level 5
Joined
Feb 12, 2006
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,345
Easy Matlab question

Hi,

I'm new to Matlab and I need to plot the following function

x[n] = 5^|n| , |n|≤12
= 0 , |n|> 12

Please help

Mark
 

Re: Easy Matlab question

here is the code for your problem. you can change the range of the plot by changing range = -15:0.1:15 line (but you must use symmetric range, I mean from -a to +a). The number in the middle (0.1) is the distance between two points (sampling distance). You can aslo change that but use same sampling distance for n and range. If you need a more general case or a solution using for loops, let me know.

range = -15:0.1:15;
size_of_range = size(range);
n = -12:0.1:12;
size_of_n = size;
x = zeros(size_of_range(2));
x((size_of_range(2)-size_of_n(2))/2+1size_of_range(2)+size_of_n(2))/2) = 5.^(abs);
plot(range,x)
 

Re: Easy Matlab question

%One simple way of doing is it like

n=-100:100;
x=0:length;

for i=1:length
if abs(n(i))<=12
x(i)=5^n(i);
end
if abs(n(i))>12
x(i)=0;
end
end

plot(x);
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…