# Plotting values by defining a function plotter

1 view (last 30 days)
Kunal Jain on 26 Feb 2022
Commented: Kunal Jain on 26 Feb 2022
Getting error in a function named plotter.
ERROR:
plotter( 1,8,1 )
Index in position 1 exceeds array bounds. Index must not exceed 1.
Error in plotter (line 18)
u(j)=-Interval(4,i);
CODE:
function plotter( Interval,k,b )
%plots the various values of solutions u1(t) and u2(t) versus time
arrsize=18000;
x=zeros(1,arrsize-1);
x1=zeros(1,arrsize-1);
u=zeros(1,arrsize-1);
%%creating x as linear x-axis
for i=1:arrsize-1
x(i)=i*b/arrsize;
end
for j=1:arrsize-1
for i=1:k
if((x(j)>=(((i-1)*b/k)))&&(x(j)<((i)*b/k)))
x1(j)=Interval(1,i);
u(j)=-Interval(4,i);
%%%%%%%%%%%%%%%%%%%%%%%%
%% because u(t)=-p2(t) %%
%%%%%%%%%%%%%%%%%%%%%%%
end
end
end
figure(1);
plot(x,max(x1,0),'DisplayName','x1 (first state variable)'); hold on;
plot(x,max(u,0),'DisplayName','u (control variable)');
axis([0 b 0 6.2]); % This give the range on x-axis and y-axis in the graph
legend('show');
set(gca,'FontSize',12);
end

KSSV on 26 Feb 2022
Your inputs to the function are not correct. The function:
plotter( Interval,k,b )
Interval should be an array/ vector.
Example:
Interval = rand(1,8) ; % should be an vector
k = 1;
b = 1;
plotter( Interval,k,b )
Kunal Jain on 26 Feb 2022
I got it that Interval should be an array/vector.