Plotting In MATLAB - matlab

I have a code that I would like to add an additional plot inside. I want to plot the relative error of two of my functions. I am unsure where in the loop to place this so that it doesn't give me an error message and plots all three cases as single graphs. Also, my relative error code may have some mistakes, which is what may be causing the problem.
This is the relative error code:
rel_error = (y_exact1 - Y(:,2)')./y_exact; %relative error
figure()
plot(T,rel_error,'r')
This is the function I need to add it into
function ivp1()
clear;clc;close all;
t=linspace(0,2.5);
K=[.02 .1 1.5];
for i=1:3
k =K(i);
[T,Y] = ode45(#prblm1_fun,t,0); %Solving for the approximate solution to the IVP
figure()
plot(T,Y)
hold on
y_exact1 = 1/(k^2+pi^2)*(pi*exp(k*t)-pi*cos(pi*t)-k*sin(pi*t));
y_exact2 = 1/(2*k)*(exp(k*(t-1))-1) + pi/(k^2+pi^2)*(exp(k*t) + exp(k* (t-1)));
y_exact3 = 1/2/k*(exp(k*(t-1))-exp(k*(t-2))) + pi/(k^2+pi^2)*(exp(k*t) + exp(k*(t-1))) + 1/2/(k-1)*(exp(k*(t-2)) - exp(t-2));
for i=1:length(t)
if t(i)<1
plot(t(i),y_exact1(i),'mo')
hold on
elseif t(i)<2
plot(t(i),y_exact2(i),'mo')
hold on
else
plot (t(i),y_exact3(i),'mo')
hold on
end
end
end
function dy= prblm1_fun(t,y) %This is the function of the IVP for varying values of t
dy = zeros(1,1);
if t < 1
dy(1)= y(1)*k + sin(pi*t);
elseif t < 2
dy(1)= y(1)*k + 0.5;
else
dy(1)= y(1)*k + exp(t-2)/2;
end
end
end
This is the desired result for one of the k values:

Two erroneous places:
1) Y is a 100x1 matrix from the result of ode45. Trying to access Y(:,2) leads into a out-of-boundary error.
2) y_exact is not defined. You only have y_exact1, y_exact2 and y_exact3.
Sample code:
function ivp1()
clear;clc;close all;
t=linspace(0,2.5);
K=[.02 .1 1.5];
for i=1:3
k =K(i);
[T,Y] = ode45(#prblm1_fun,t,0); %Solving for the approximate solution to the IVP
figure()
plot(T,Y)
hold on
y_exact1 = 1/(k^2+pi^2)*(pi*exp(k*t)-pi*cos(pi*t)-k*sin(pi*t));
y_exact2 = 1/(2*k)*(exp(k*(t-1))-1) + pi/(k^2+pi^2)*(exp(k*t) + exp(k* (t-1)));
y_exact3 = 1/2/k*(exp(k*(t-1))-exp(k*(t-2))) + pi/(k^2+pi^2)*(exp(k*t) + exp(k*(t-1))) + 1/2/(k-1)*(exp(k*(t-2)) - exp(t-2));
for j=1:length(t)
if t(j)<1
y_exact = y_exact1(j);
elseif t(j)<2
y_exact = y_exact2(j);
else
y_exact = y_exact3(j);
end
plot(t(j),y_exact,'mo')
end
rel_error = (y_exact1 - Y(:,1)')./y_exact; %relative error
plot(T,rel_error,'r')
end
function dy= prblm1_fun(t,y) %This is the function of the IVP for varying values of t
dy = zeros(1,1);
if t < 1
dy(1)= y(1)*k + sin(pi*t);
elseif t < 2
dy(1)= y(1)*k + 0.5;
else
dy(1)= y(1)*k + exp(t-2)/2;
end
end
end
Sample output:

Related

Unsure about how to use event function in Matlab

I am trying to plot a state-space diagram, as well as a time-history diagram, of a dynamical system. There's a catch, though. The state-space is divided into two halves by a plane located at x1 = 0. The state-space axes are x1, x2, x3. The x1 = 0 plane is parallel to the x2/x3 plane. The state-space above the x1 = 0 plane is described by the ODEs in eqx3, whereas the state-space below the x1 = 0 plane is described by the ODEs in eqx4.
So, there is a discontinuity on the plane x1 = 0. I have a vague understanding that an event function (function [value,isterminal,direction] = myEventsFcn(t,y)) should be used, but I do not know what values to give to "value", "isterminal", and "direction".
In my code below, I have an initial condition for eqx3, and another initial condition for eqx4. The initial condition for eqx3 is in the upper half of the state-space (x1 > 0). The orbit then hits the x1 = 0 plane and there is a discontinuity, and the eqx4 trajectory begins on this plane, but from a different point from where eqx3 ended.
Can this be done? How do I put it into a code? Do I stop the integration when the orbit reaches the plane x1 = 0?
eta = 0.05;
omega = 25;
tspan = [0,50];
initcond = [2, 3, 4]
[t,x] = ode45(#(t,x) eqx3(t,x,eta, omega), tspan, initcond);
initcond1 = [0, 1, 1]
[t,y] = ode45(#(t,y) eqx4(t,y,eta, omega), tspan, initcond1);
plot3(x(:,1), x(:,2), x(:,3),y(:,1), y(:,2), y(:,3))
xlabel('x1')
ylabel('x2')
zlabel('x3')
%subplot(222)
%plot(t, x(:,1), t,x(:,2),t,x(:,3),'--');
%xlabel('t')
function xdot = eqx3(t,x,eta,omega)
xdot = zeros(3,1);
xdot(1) = -(2*eta*omega + 1)*x(1) + x(2) - 1;
xdot(2) = -(2*eta*omega + (omega^2))*x(1) + x(3) + 2;
xdot(3) = -(omega^2)*x(1) + x(2) - 1;
end
function ydot = eqx4(t,y,eta,omega)
ydot = zeros(3,1);
ydot(1) = -(2*eta*omega + 1)*y(1) + y(2) + 1;
ydot(2) = -(2*eta*omega + (omega^2))*y(1) + y(3) - 2;
ydot(3) = -(omega^2)*y(1) + y(2) + 1;
end
function [value,isterminal,direction] = myEventsFcn(t,y)
value = 0
isterminal = 1
direction = 1
end
Without events, using a close-by smooth system
First, as the difference between the systems is the addition or subtraction of a constant vector it is easy to find an approximate smooth version of the system using some sigmoid function like tanh.
eta = 0.05;
omega = 25;
t0=0; tf=4;
initcond = [2, 3, 4];
opt = odeset('AbsTol',1e-11,'RelTol',1e-13);
opt = odeset(opt,'MaxStep',0.005); % in Matlab: opt = odeset('Refine',10);
[t,x] = ode45(#(t,x) eqx34(t,x,eta, omega), [t0, tf], initcond, opt);
clf;
subplot(121);
plot3(x(:,1), x(:,2), x(:,3));
xlabel('x1');
ylabel('x2');
zlabel('x3');
subplot(122);
plot(t, 10.*x(:,1), t,x(:,2),':',t,x(:,3),'--');
xlabel('t');
function xdot = eqx34(t,x,eta,omega)
S = tanh(1e6*x(1));
xdot = zeros(3,1);
xdot(1) = -(2*eta*omega + 1)*x(1) + x(2) - S;
xdot(2) = -(2*eta*omega + (omega^2))*x(1) + x(3) + 2*S;
xdot(3) = -(omega^2)*x(1) + x(2) - S;
end
resulting in the plots
As is visible, after t=1.2 the solution is essentially constant with x(1)=0 and the other coordinates close to zero.
With events
If you want to use the event mechanism, make ODE and event function dependent on a sign parameter S denoting the phase and the direction of the zero crossing.
eta = 0.05;
omega = 25;
t0=0; tf=4;
initcond = [2, 3, 4];
opt = odeset('AbsTol',1e-10,'RelTol',1e-12,'InitialStep',1e-6);
opt = odeset(opt,'MaxStep',0.005); % in Matlab: opt = odeset('Refine',10);
T = [t0]; TE = [];
X = [initcond]; XE = [];
S = 1; % sign of x(1)
while t0<tf
opt = odeset(opt,'Events', #(t,x)myEventsFcn(t,x,S));
[t,x,te,xe,ie] = ode45(#(t,x) eqx34(t,x,eta, omega, S), [t0, tf], initcond, opt);
T = [ T; t(2:end) ]; TE = [ TE; te ];
X = [ X; x(2:end,:)]; XE = [ XE; xe ];
t0 = t(end);
initcond = x(end,:);
S = -S % alternatively = 1-2*(initcond(1)<0);
end
disp(TE); disp(XE);
subplot(121);
hold on;
plot3(X(:,1), X(:,2), X(:,3),'b-');
plot3(XE(:,1), XE(:,2), XE(:,3),'or');
hold off;
xlabel('x1');
ylabel('x2');
zlabel('x3');
subplot(122);
plot(T, 10.*X(:,1), T,X(:,2),':',T,X(:,3),'--');
xlabel('t');
function xdot = eqx34(t,x,eta,omega,S)
xdot = zeros(3,1);
xdot(1) = -(2*eta*omega + 1)*x(1) + x(2) - S;
xdot(2) = -(2*eta*omega + (omega^2))*x(1) + x(3) + 2*S;
xdot(3) = -(omega^2)*x(1) + x(2) - S;
end
function [value,isterminal,direction] = myEventsFcn(t,x,S)
value = x(1)+2e-4*S;
isterminal = 1;
direction = -S;
end
The solution enters a sliding mode for 1.36 < t < 1.43 where (theoretically) x(1)=0 and the vector field points to the other phase from both sides. The theoretical solution is to take a linear combination that sets the first component to zero, so that the resulting direction is tangential to the separating surface. In the first variant above the sigmoid achieves something like this automatically. Using events one could add additional event functions that test the vector field for these conditions, and when they cease to persist.
A quick solution is to thicken the boundary surface, that is, test for x(1)+epsilon*S==0 so that the solution has to cross the boundary surface before triggering the event. In sliding mode, it will be immediately pushed back, giving a ping-pong or zigzag motion. epsilon has to be small to not perturb the solution too much, however not too small to avoid the triggering of too many events. With epsilon=2e-4 octave gives the following solution in a close-up to the sliding interval
The octave solver, and in some way also Matlab, will not trigger a terminal event if it happens in the first integration step. For that reason the InitialStep option was set to a rather small value, it should be about 0.01*epsilon. The full solution looks now similar to the one obtained in the first variant

High performance computation of mean 2D-Euclidian-distance

Let's say I have a position matrix P with dimensions 10x2, where the first column contains x values and second column the corresponding y values. I want the mean of the lengths of the positions. The way I have done this so far is with the following code:
avg = sum( sqrt( P(:,1).^2 + P(:,2).^2))/10);
I've been told that the integral function integral2 is much faster and more precise for this task. How can I use integral2 to compute the mean value?
Just so this question doesn't remain unanswered:
function q42372466(DO_SUM)
if ~nargin % nargin == 0
DO_SUM = true;
end
% Generate some data:
P = rand(2E7,2);
% Correctness:
R{1} = m1(P);
R{2} = m2(P);
R{3} = m3(P);
R{4} = m4(P);
R{5} = m5(P);
R{6} = m6(P);
for ind1 = 2:numel(R)
assert(abs(R{1}-R{ind1}) < 1E-10);
end
% Benchmark:
t(1) = timeit(#()m1(P));
t(2) = timeit(#()m2(P));
t(3) = timeit(#()m3(P));
t(4) = timeit(#()m4(P));
t(5) = timeit(#()m5(P));
t(6) = timeit(#()m6(P));
% Print timings:
disp(t);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Original method:
function out = m1(P)
if DO_SUM
out = sum( sqrt( P(:,1).^2 + P(:,2).^2))/max(size(P));
else
out = mean( sqrt( P(:,1).^2 + P(:,2).^2));
end
end
% pdist2 method:
function out = m2(P)
if DO_SUM
out = sum(pdist2([0,0],P))/max(size(P));
else
out = mean(pdist2([0,0],P));
end
end
% Shortened method #1:
function out = m3(P)
if DO_SUM
out = sum(sqrt(sum(P.*P,2)))/max(size(P));
else
out = mean(sqrt(sum(P.*P,2)));
end
end
% Shortened method #2:
function out = m4(P)
if DO_SUM
out = sum(sqrt(sum(P.^2,2)))/max(size(P));
else
out = mean(sqrt(sum(P.^2,2)));
end
end
% hypot
function out = m5(P)
if DO_SUM
out = sum(hypot(P(:,1),P(:,2)))/max(size(P));
else
out = mean(hypot(P(:,1),P(:,2)));
end
end
% (a+b)^2 formula , Divakar's idea
function out = m6(P)
% Since a^2 + b^2 = (a+b)^2 - 2ab,
if DO_SUM
out = sum(sqrt(sum(P,2).^2 - 2*prod(P,2)))/max(size(P));
else
out = mean(sqrt(sum(P,2).^2 - 2*prod(P,2)));
end
end
end
Typical result on my R2016b + Win10 x64:
>> q42372466(0) % with mean()
0.1165 0.1971 0.2167 0.2161 0.1719 0.2375
>> q42372466(1) % with sum()
0.1156 0.1979 0.2233 0.2181 0.1610 0.2357
Which means that your method is actually the best of the above, by a considerable margin! (Honestly - didn't expect that!)

Error in FDM for a coupled PDEs

Here is the code which is trying to solve a coupled PDEs using finite difference method,
clear;
Lmax = 1.0; % Maximum length
Wmax = 1.0; % Maximum wedth
Tmax = 2.; % Maximum time
% Parameters needed to solve the equation
K = 30; % Number of time steps
n = 3; % Number of space steps
m =30; % Number of space steps
M = 2;
N = 1;
Pr = 1;
Re = 1;
Gr = 5;
maxn=20; % The wave-front: intermediate point from which u=0
maxm = 20;
maxk = 20;
dt = Tmax/K;
dx = Lmax/n;
dy = Wmax/m;
%M = a*B1^2*l/(p*U)
b =1/(1+M*dt);
c =dt/(1+M*dt);
d = dt/((1+M*dt)*dy);
%Gr = gB*(T-T1)*l/U^2;
% Initial value of the function u (amplitude of the wave)
for i = 1:n
if i < maxn
u(i,1)=1.;
else
u(i,1)=0.;
end
x(i) =(i-1)*dx;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for j = 1:m
if j < maxm
v(j,1)=1.;
else
v(j,1)=0.;
end
y(j) =(j-1)*dy;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for k = 1:K
if k < maxk
T(k,1)=1.;
else
T(k,1)=0.;
end
z(k) =(k-1)*dt;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Value at the boundary
%for k=0:K
%end
% Implementation of the explicit method
for k=0:K % Time loop
for i=1:n % Space loop
for j=1:m
u(i,j,k+1) = b*u(i,j,k)+c*Gr*T(i,j,k+1)+d*[((u(i,j+1,k)-u(i,j,k))/dy)^(N-1)*((u(i,j+1,k)-u(i,j,k))/dy)]-d*[((u(i,j,k)-u(i,j-1,k))/dy)^(N-1)*((u(i,j,k)-u(i,j-1,k))/dy)]-d*[u(i,j,k)*((u(i,j,k)-u(i-1,j,k))/dx)+v(i,j,k)*((u(i,j+1,k)-u(i,j,k))/dy)];
v(i,j,k+1) = dy*[(u(i-1,j,k+1)-u(i,j,k+1))/dx]+v(i,j-1,k+1);
T(i,j,k+1) = T(i,j,k)+(dt/(Pr*Re))*{(T(i,j+1,k)-2*T(i,j,k)+T(i,j-1,k))/dy^2-Pr*Re{u(i,j,k)*((T(i,j,k)-T(i-1,j,k))/dx)+v(i,j,k)*((T(i,j+1,k)-T(i,j,k))/dy)}};
end
end
end
% Graphical representation of the wave at different selected times
plot(x,u(:,1),'-',x,u(:,10),'-',x,u(:,50),'-',x,u(:,100),'-')
title('graphs')
xlabel('X')
ylabel('Y')
But I am getting this error
Subscript indices must either be real positive integers or logicals.
I am trying to implement this
with boundary conditions
Can someone please help me out!
Thanks
To be quite honest, it looks like you started with something that's way over your head, just typed everything down in one go without thinking much, and now you are surprised that it doesn't work...
In the future, please break down problems like these into waaaay smaller chunks that you can individually plot, check, test, etc. Better yet, try simpler problems first (wave equation, heat equation, ...), gradually working your way up to this.
I say this so harshly, because there were quite a number of fairly basic things wrong with your code:
you've used braces ({}) and brackets ([]) exactly as they are written in the equation. In MATLAB, braces are a constructor for a special container object called a cell array, and brackets are used to construct arrays and matrices. To group things like in the equation, you always have to use parentheses (()).
You had quite a number of parentheses wrong, which became apparent when I re-grouped and broke up those huge unintelligible lines into multiple lines that humans can actually read with understanding
you forgot to take the absolute values in the 3rd and 4th terms of u
you looped over k = 0:K and j = 1:m and then happily index everything with k and j-1. MATLAB is 1-based, meaning, the first element of anything is element 1, and indexing with 0 is an error
you've initialized 3 vectors u, v and T, but then index those in the loop as if they are 3D arrays
Now, I've managed to come up with the following code, which runs OK and at least more or less agrees with the equations shown. But I think it still doesn't make much sense because I get only zeros out (except for the initial values).
But, with this feedback, you should be able to correct any problems left.
Lmax = 1.0; % Maximum length
Wmax = 1.0; % Maximum wedth
Tmax = 2.; % Maximum time
% Parameters needed to solve the equation
K = 30; % Number of time steps
n = 3; % Number of space steps
m = 30; % Number of space steps
M = 2;
N = 1;
Pr = 1;
Re = 1;
Gr = 5;
maxn = 20; % The wave-front: intermediate point from which u=0
maxm = 20;
maxk = 20;
dt = Tmax/K;
dx = Lmax/n;
dy = Wmax/m;
%M = a*B1^2*l/(p*U)
b = 1/(1+M*dt);
c = dt/(1+M*dt);
d = dt/((1+M*dt)*dy);
%Gr = gB*(T-T1)*l/U^2;
% Initial value of the function u (amplitude of the wave)
u = zeros(n,m,K+1);
x = zeros(n,1);
for i = 1:n
if i < maxn
u(i,1)=1.;
else
u(i,1)=0.;
end
x(i) =(i-1)*dx;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
v = zeros(n,m,K+1);
y = zeros(m,1);
for j = 1:m
if j < maxm
v(1,j,1)=1.;
else
v(1,j,1)=0.;
end
y(j) =(j-1)*dy;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
T = zeros(n,m,K+1);
z = zeros(K,1);
for k = 1:K
if k < maxk
T(1,1,k)=1.;
else
T(1,1,k)=0.;
end
z(k) =(k-1)*dt;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Value at the boundary
%for k=0:K
%end
% Implementation of the explicit method
for k = 2:K % Time loop
for i = 2:n % Space loop
for j = 2:m-1
u(i,j,k+1) = b*u(i,j,k) + ...
c*Gr*T(i,j,k+1) + ...
d*(abs(u(i,j+1,k) - u(i,j ,k))/dy)^(N-1)*((u(i,j+1,k) - u(i,j ,k))/dy) - ...
d*(abs(u(i,j ,k) - u(i,j-1,k))/dy)^(N-1)*((u(i,j ,k) - u(i,j-1,k))/dy) - ...
d*(u(i,j,k)*((u(i,j ,k) - u(i-1,j,k))/dx) +...
v(i,j,k)*((u(i,j+1,k) - u(i ,j,k))/dy));
v(i,j,k+1) = dy*(u(i-1,j,k+1)-u(i,j,k+1))/dx + ...
v(i,j-1,k+1);
T(i,j,k+1) = T(i,j,k) + dt/(Pr*Re) * (...
(T(i,j+1,k) - 2*T(i,j,k) + T(i,j-1,k))/dy^2 - Pr*Re*(...
u(i,j,k)*((T(i,j,k) - T(i-1,j,k))/dx) + v(i,j,k)*((T(i,j+1,k) - T(i,j,k))/dy))...
);
end
end
end
% Graphical representation of the wave at different selected times
figure, hold on
plot(x, u(:, 1), '-',...
x, u(:, 10), '-',...
x, u(:, 50), '-',...
x, u(:,100), '-')
title('graphs')
xlabel('X')
ylabel('Y')

MATLAB: data points won't connect?

I only have about two weeks of experience with programming/Matlab, so I'm just a beginner. In my code I would like to plot mu as a function of alpha. When I display mu it shows the 10 values of mu for each value of alpha. However, when I plot the graph it gives the values of mu as seperate points. But I want the points to be connected with just one line. How can I solve this problem?
n=40; %number of months
p=0.23; %probability of success
num_of_simulations=100;
s=rng; x = rand(1,n)<p;
rng(s);
hold on;
for alpha=0.01:0.01:0.1;
for i=1:num_of_simulations
x = rand(1,n)<p;
S0=5000; %initial value
Y(1)=S0*alpha; %deposit
for k=1
if x(1,1)==1;
S(1, i)=S0+2*Y(1);
else
S(1, i)=S0-Y(1);
end
end
for k=2:n
Y(k)=S(k-1, i)*alpha;
if x(1,k)==1;
S(k, i)=S(k-1, i)+2*Y(k);
else
S(k, i)=S(k-1, i)-Y(k);
end
end
Sn(i)=S(n,i); %end value for each simulations
end
mu=mean(Sn);
disp(mu);
plot(alpha,mu);
end
The reason your points aren't connected is because you plot each point separately. If we take a different approach and take alpha = 0.01:0.01:0.1; out of the for loop definition and then change the for loop definition to for j=1:numel(alpha) we can still loop over every element of alpha. Now we need to change each use of alpha in the loop to alpha(j) so that we are referring to the current element of alpha and not every element. Following on from this we need to change mu to mu(j). What this means is that when the entire loop has finished we have all of the values of alpha and mu stored and 1 call to plot(alpha, mu) will plot the data with the points connected as in
This also enables us to remove hold on; too as we only plot once.
I've included the complete edited code here for you to see. The changes are minuscule and should make sense.
clear all
close all
n = 40; %number of months
p = 0.23; %probability of success
num_of_simulations = 100;
s = rng;
x = rand(1, n) < p;
rng(s);
alpha = 0.01:0.01:0.1;
for j = 1:numel(alpha)
for i = 1:num_of_simulations
x = rand(1, n) < p;
S0 = 5000; %initial value
Y(1) = S0*alpha(j); %deposit
for k = 1
if x(1, 1) == 1;
S(1, i) = S0 + 2*Y(1);
else
S(1, i) = S0 - Y(1);
end
end
for k = 2:n
Y(k) = S(k-1, i)*alpha(j);
if x(1, k) == 1;
S(k, i) = S(k-1, i) + 2*Y(k);
else
S(k, i) = S(k-1, i) - Y(k);
end
end
Sn(i) = S(n, i); %end value for each simulations
end
mu(j) = mean(Sn);
disp(mu(j));
end
plot(alpha, mu);

Solving a three-variable implicit function using MATLAB

I want to plot n vs theta1 from the function tan(n*theta1) + tan(n*theta2) + tan(n*thetas)= 0 in MATLAB; where n, theta1 and theta2 are variables and thetas is given. Can anyone give any pointers how to do so ?
I have tried using ezplot. It didn't work.
Next, I used this bit of code,
c = 1;
thetas = pi/4;
for n = 1.01:0.01:3.50
for theta1 = pi/2 : 0.01 : 5*pi/6
for theta2 = 0: 0.01: pi/2
if(tan(n*theta1) + tan(n*theta2) + tan(n*thetas) >= -0.0001 && tan(n*theta1) + tan(n*theta2) + tan(n*thetas) <= 0.0001 && (n*theta1) ~= pi/2 ...
&& (n*theta2) ~= pi/2 && (n*thetas) ~= pi/2)
ns(c) = n;
theta1s(c) = theta1;
theta2s(c) = theta2;
c = c + 1;
end
end
end
end
And I tried to plot the result using the plot command. Didn't work out either. Mostly the plot should come as three different curves for every given value of thetas.
Can anyone help ?
Something like this might work? -
%%// Data (Random numbers)
thetas_array = [2 4 6]; %// Put different thetas here
theta2 = 2:2:20;
n = 1:10;
figure,
for k = 1:numel(thetas_array)
thetas = thetas_array(k);
%%// Since tan(n*theta1) + tan(n*theta2) + tan(n*thetas)= 0;
theta1 = (1./n).*atan(- tan(n.*theta2) - tan(n.*thetas));
subplot(3,1,k), plot(n,theta1), xlabel('n'), ylabel(strcat('theta1 [thetas = ',num2str(thetas),']'));
end
Output