Plotting values by defining a function plotter

2 views (last 30 days)
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

Accepted Answer

KSSV
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 )
  1 Comment
Kunal Jain
Kunal Jain on 26 Feb 2022
Thanks for answering
I got it that Interval should be an array/vector.

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!