illegal plot error when trying to use subplot - matlab

I am trying to use subplot, but got an error message I do not know how to correct.
(Also, since only the value of w varies, is there a more compact way to write this code rather than saying "cos(stuff*t") ten times in a row? Thanks.)
my current code is
clear all
clc
t = 0:0.001:40;
% there are 10 values of w to show, so make a 2x5 grid to show them all via
% subplot (2,5, blank)
x1=cos(pi/16*t);
x2=cos(pi/8*t);
x3=cos(pi/4*t);
x4=cos(pi/2*t);
x5=cos(pi*t);
x6=cos(15*pi/8*t);
x7=cos(2*pi*t);
x8=cos(5*pi/2*t);
x9=cos(3*pi*t);
x10=cos(4*pi*t);
subplot(2,5,x1), plot(t,x,1), subplot(2,5,x2), plot(t,x,2), subplot(2,5,x3),
plot(t,x,3), subplot(2,5,x4), plot(t,x,4), subplot(2,5,x5), plot(t,x,5),
subplot(2,5,x6), plot(t,x,6), subplot(2,5,x7), plot(t,x,7), subplot(2,5,x8),
plot(t,x,8), subplot(2,5,x9), plot(t,x,9), subplot(2,5,x10), plot(t,x,10);
%Error using subplot (line 330);
%Illegal plot number;
%Error in (line 19);
%subplot(2,5,x1), plot(t,x,1);

You just mixed the parameters of plot and subplot.
Read subplot documentation.
Here is a corrected version of you code:
clear all
clc
t = 0:0.001:40;
% there are 10 values of w to show, so make a 2x5 grid to show them all via
% subplot (2,5, blank)
x1=cos(pi/16*t);
x2=cos(pi/8*t);
x3=cos(pi/4*t);
x4=cos(pi/2*t);
x5=cos(pi*t);
x6=cos(15*pi/8*t);
x7=cos(2*pi*t);
x8=cos(5*pi/2*t);
x9=cos(3*pi*t);
x10=cos(4*pi*t);
subplot(2,5,1);
plot(t,x1);
subplot(2,5,2);
plot(t,x2);
subplot(2,5,3);
plot(t,x3);
subplot(2,5,4);
plot(t,x4);
subplot(2,5,5);
plot(t,x5);
subplot(2,5,6);
plot(t,x6);
subplot(2,5,7);
plot(t,x7);
subplot(2,5,8);
plot(t,x8);
subplot(2,5,9);
plot(t,x9);
subplot(2,5,10);
plot(t,x10);
Here is how to do it using for loops:
clear all
clc
t = 0:0.001:40;
% there are 10 values of w to show, so make a 2x5 grid to show them all via
% subplot (2,5, blank)
W = [1/16, 1/8, 1/4, 1/2, 1, 15/8, 2, 5/2, 3, 4]*pi;
%X Replaces x1 to x10
X = zeros(10, length(t));
%Fill X with 10 rows (X(1, :) matches x1, X(2, :) matches x2...)
for i = 1:10
X(i, :) = cos(W(i)*t);
end
%Plot using a for loop
for i = 1:10
subplot(2,5,i);
plot(t, X(i, :));
end
You can even do it without the first for loop:
t = 0:0.001:40;
W = [1/16, 1/8, 1/4, 1/2, 1, 15/8, 2, 5/2, 3, 4]*pi;
%Fill X with 10 rows (X(1, :) matches x1, X(2, :) matches x2...)
X = cos(W'*t);
%Plot using a for loop
for i = 1:10
subplot(2,5,i);
plot(t, X(i, :));
end

Related

How to find a value which is bigger than the maximum point from point set? (Function to find Cubic Spline Interpolation by Matlab)

I have written a Matlab code to construct a Cubic Runout Spline, with a figure to display my data. But how can i show a data which is not in my data group ex.f(2010) in my figure. I have an idea. I can show t is valid after 2000, which t=2010, but I have no idea to start it.
clear; clc;
t= [1850, 1875, 1900, 1925, 1950, 1975, 2000];
y= [285.2, 288.6, 295.7, 305.3, 311.3, 331.36, 369.64];
N= length(t); %number of points I want
n=N-1 ; % number of subintervals
h=(t(N)-t(1))/n; %step size
A=[1,1,1,0],B=[2,0,0,0,2],C=[0,1,1,1];
Trid=diag(4*ones(1,n-1))+diag(A,-1)+diag(B)+diag(C,1);
for i=1:n-1
f(i)= 6/h^2*(y(i+2)-2*y(i+1)+y(i));
end
f=f';
w=inv(Trid)*f;%since sigma 1 and sigma n+1 are both 0, we need to add 0 in the beginning and also in the end of then matrix
sigma=[0;w;0];%it is a nx1 matrix, be careful.
for i=1:n
d(i)=y(i);
b(i)=sigma(i)/2;
a(i)=(sigma(i+1)-sigma(i))/(6*h);
c(i)=(y(i+1)-y(i))/h-h/6*(2*sigma(i)+sigma(i+1));
end
r= 25; %subsubintervals for t ex. between 1850 and 1875, here i seperate it into 1 years per slot
hh=h/r; %step size of subsubintervals
x=t(1):hh:t(N);
for i=1:n
for j=r*(i-1)+1:r*i
s(j)=a(i)*(x(j)-t(i))^3+b(i)*(x(j)-t(i))^2+c(i)*(x(j)-t(i))+d(i);
end
end
s(r*n+1)=y(N);
plot(t,y,'o')
hold on
plot(x,s,'-x')
hold off
What you are asking is extrapolation. You did the interpolation, which is good. For the extrapolation, just continue the last segment's cubic equation.
s(r*n+1)=y(N);
s_orig_len = length(s); %% new
plot(t,y,'o')
hold on
plot(x,s,'-x')
% hold off %%% there is more to plot!
% %% All new lines below
i_last = 6;
i_next = 7;
t = [t 2026];
x_extrapolation = x(end):t(end);
x = [x x_extrapolation];
for j = r*(i_next-1)+1:r*i_next + 3
s(j)=a(i_last)*(x(j)-t(i_last))^3+b(i_last)*(x(j)-t(i_last))^2+c(i_last)*(x(j)-t(i_last))+d(i_last);
end
plot(x_extrapolation, s(s_orig_len+1:end), '-b', 'LineWidth',2)
hold off;
I hope this helps.

How to plot multiple lines in one plot and save them

Question 1 SOLVED
I am working on the following simulation:
x = [8 9 7 6 5];`
if isvector(x)
for i=1:length(x)
% simulation gives me a matrix y,t,z, (c by 5)
% where size(y,1)=size(z,1)=size(t,1)= lenght(x)=5
% size(y,2)=c
% a plot will collect all lines:
% for x=8 there are 3 lines ( the first row of each matrix `y`, `t`, `z`
% for x=9 there are 3 lines ( the second row of each matrix `y`, `t`, `z`
% ...
% for x=5 there are 3 lines ( the 5th row of each matrix `y`, `t`, `z`
end
end
Let me show an example:
y = rand(5,8)
t = rand(5,8)
z = rand(5,8)
To plot I have started with:
% I was using the initial loop:
if isvector(x)
for i=1:length(x)
% simulation gives me a matrix y,t,z, (c by 5)
%% plots
h(1)=figure;
plot (c,y(i,:));
grid on;
hold on;
plot (c,t(i,:));
plot (c,z(i,:));
hold off;
end
end
As a result, MATLAB gives me 3 figures, but I have expected only one figure with multiple lines. I start from the inside of the initial loop and create a new loop, but it doesn't help me. How to fix it? How to plot all lines (in this example, all 15 lines (#x(i) = 5, #array = 3))?
If you need that loop for further analysis, just move the figure() outside the loop. Otherwise, you could also plot all lines from a single plot() call:
x = [8 9 7 6 5];
c = 1:8;
y = rand(5, 8);
t = rand(5, 8);
z = rand(5, 8);
% Loop approach, move figure() outside the loop
if isvector(x)
figure(1);
hold on;
for i = 1:length(x)
plot(c, y(i, :));
plot(c, t(i, :));
plot(c, z(i, :));
end
hold off;
grid on;
end
% Plot everything with one plot() call
if isvector(x)
figure(2);
plot(c, [y; t; z]);
grid on;
end
The outputs are the same except for the lines' color, which is due to the order of the plotting (first line of y, t, z, second line of ..., and so on vs. all lines from y, all lines from t, and so on.
Hope that helps!
EDIT: To plot all three lines for each x(i) in separate figures, you could use this loop approach:
x = [8 9 7 6 5];
c = 1:8;
y = rand(5, 8);
t = rand(5, 8);
z = rand(5, 8);
if isvector(x)
for i = 1:length(x)
figure(i);
hold on;
plot(c, y(i, :));
plot(c, t(i, :));
plot(c, z(i, :));
hold off;
grid on;
end
end

How do I plot the solutions and errors obtained through Jacobi Iteration?

I am trying to create a figure showing the solution I obtained through Jacobi iteration along with the true solution, as well as the error of the Jacobi solution.
The figure I'm trying to create should consist of two plots.
I used the subplot command, to split the figure into
an upper and lower axes and I wrote the for loop that calculates the Jacobi iterations and the error. The loop is going to iterate 400 times using x0 as the initial guess. Before this, I calculated the true solution to the system Ax = b.
N = 30;
iter = 400;
A = toeplitz([-2 1 zeros(1, N-2)], [-2 1 zeros(1, N-2)]);
bk = ones(N,1);
for jj = 1:N
bk(jj) = cos(5*jj) + (1/2)*sin(7*jj);
end
x = A\bk;
D = diag(diag(A));
T = A - D;
x0 = zeros(N,1);
error = zeros(iter,1);
M = -D\T;
g = D\bk;
for nn = 1:iter
x0 = M*x0 + g;
error(nn) = norm(x - x0,2);
end
subplot(2,1,1)
plot(x0(1:N,1),'ro');
ylabel('Solution','FontSize',22);
title('Solution by Jacobi Iteration','FontSize',22);
xlim([0 pi]);
ylim([-5 5]);
xticks(0:0.5:3);
yticks(-5:5:5);
subplot(2,1,2)
plot(error(1:N),'ro')
ylabel('Error','FontSize',22);
xlabel('t','FontSize',22);
xlim([0 pi]);
ylim([0 0.1]);
xticks(0:0.5:3);
yticks(0:0.05:0.1);
The upper window should show the true solution in red circles connected by solid lines. The lower window show show the error as red
circles connected by dotted lines. When I ran my code, only 3 red circles appeared in the upper window and nothing was plotted in the lower window. I'm still bad at plotting iterations of a loop. Can someone help me with plotting the solutions and errors I calculated?
The xlim and ylim statements are not representative of the data.
x0 and x have N elements (30 here), and the elements of x and x0 span -2 to 2 in this setup.
error has iter elements (400 here), and the elements of error go from 4 to about 0.01.
For these plots, the element index maps to the horizontal x-axis, and their values to the y-axis. I think this plot setup should give you the result you desire (I probably changed more than actually needed):
subplot(2,1,1);
plot(1:N, x0(1:N,1), 'ro', 1:N, x,'k+');
title('Solution by Jacobi Iteration','FontSize',22);
ylabel('Solution','FontSize',22);
xlim([1, N]);
ylim([-3, 3]);
xticks(1:N);
yticks(-3:0.5:3);
subplot(2,1,2)
semilogy(1:iter, error(1:iter),'ro')
ylabel('Error','FontSize',22);
xlabel('t','FontSize',22);
xlim([1 iter]);
ylim([0 4]);
xticks(0:25:400);

Solve quadratic equations without individually specifying cofficients

I am trying to solve equations like:
3*(3x-12)/(x+3)-2*(2x+3)/(3x-1) = 5
This is the code that I use:
eqn1 = 3*(3*X-12)/(X+3)-2*(2*X+3)/(3*X-1) == 5;
sol = solve(eqn1, X);
xSol = sol.X
This is the error that I get:
Error using sym/subsref
Too many output arguments.
The first thing I'd suggest is performing a graphical solution:
% Define the function:
f = #(X)3*(3*X-12)./(X+3)-2*(2*X+3)./(3*X-1)-5;
% Plot the function (solve graphically):
x = -30:0.1:30;
figure(); plot(x,f(x)); grid on; grid minor;
This function has vertical asymptotes and a horizontal asymptote, at x=-3, x=1/3 and y=8/3 (finding this is left as an exercise to the reader). Let's add them to the chart and zoom to the y-vicinity of 0:
hold on; plot([-3, -3, NaN, 1/3, 1/3], 600*[-1, 1, NaN, -1, 1],'--r');
plot([-30, 30], 8/3*[1 1], '--m'); ylim([-10 10]);
There appear to be two solution, one between the vertical asymptotes and another to the right of the right asymptote. We can define these regions for fzero:
% Find zeros:
z = [ fzero(f, [-3+eps(3) 1/3-eps(1/3)] ),... First solution
fzero(f, [1/3+eps, 30])]; % Second solution
(where 30 is some sufficiently large number) and we get:
z =
0.1902 21.6848

plot some data such as pairs in matlab

I want to plot some data, but I can't.
It is assumed that we have 820 rows in 2 columns, representing the x and y coordinates.
My code is as follows:
load('MyMatFileName.mat');
[m , n]=size(inputs);
s = zeros(m,2);
for m=1:820
if inputs(m,end-1)<2 & inputs(m,end)<2
x = inputs(m,end-1)
y = inputs(m,end)
plot(x,y,'r','LineWidth',1.5)
hold on
end
end
I've edited your code and added comments to explain the changes you could make, but see below I've also re-written your code to be more like how it should be done:
load('MyMatFileName.mat'); % It's assumed "inputs" is in here
% You don't use the second size output, so use a tilde ~
[m, ~] = size(inputs);
%s = zeros(m,2); % You never use s...
% Use a different variable for the loop, as m is already the size variable
% I've assumed you wanted ii=1:m rather than m=1:820
figure
hold on % Use hold on and hold off around all of your plotting
for ii=1:m
if inputs(m,end-1)<2 && inputs(m,end)<2 % Use double ampersand for individual comparison
x = inputs(m,end-1)
y = inputs(m,end)
% Include the dot . to specify you want a point, not a line!
plot(x, y, 'r.','LineWidth',1.5)
end
end
hold off
A better way of doing this whole operation in Matlab would be to vectorise your code:
load('MyMatFileName.mat');
[m, ~] = size(inputs);
x = inputs(inputs(:,end-1) < 2 & inputs(:,end) < 2, end-1);
y = inputs(inputs(:,end-1) < 2 & inputs(:,end) < 2, end);
plot(x, y, 'r.', 'linewidth', 1.5);
Note that this will plot points, if you want to plot the line, use
plot(x, y, 'r', 'linewidth', 1.5); % or plot(x, y, 'r-', 'linewidth', 1.5);