How can I create a matrix to store my data?

13 views (last 30 days)
Rose
Rose on 4 Feb 2013
I have phi and alpha scales very from 0:0.1:1 ..I want to create matrix(z) store all the data result (t_star1) for plot pcolor
clear all
clc
global phi alpha r1 r2 k1 k2 b1 b2
r1=0.01;
r2=0.01;
k1=1;
k2=0.5;
b1=0.6;
b2=0.1;
format short
ctr=0;
for phi= 0:0.1:1
for alpha =0:0.1:1
y0=[0.99, 0.01,0.0];
t=[0 :10: 4000];
[t, y]=ode15s(@n_v_w_ode,t,y0);
X=[y(:,1),y(:,2),y(:,3)];
[row_t col_y]=size(X);
t_star1=-1;
for i=1: row_t -1
if X(i,1) > X(i,2) & X(i+1,2) > X(i+1,1)
n1=X(i,1);
n2=X(i+1,1);
v1=X(i,2);
v2=X(i+1,2);
t1=t(i);
t2=t(i+1);
t_star1=((n2-n1)*t1+v1*(t2-t1)-n1*(t2-t1)-(v2-v1)*t1)/(n2-n1-v2+v1);
end
end
ctr=ctr+1
u(ctr)=phi
b(ctr)=alpha
t_star1=t_star1
z=zeros(length(phi),length(alpha))
for k=1:length(phi)
for s=1: length(alpha)
z(k,s)=t_star1
end
end
end
end
figure(1)
pcolor(z);
shading flat; colormap jet; colorbar
xlabel('\alpha')
ylabel('\phi')
title('o^o')
  1 Comment
Jan
Jan on 4 Feb 2013
I do not understand the question. The code does not get clear to me also, e.g.:
t_star1=t_star1 ?!?
t_star1 is overwritten in each iteration inside the "for i" loop, when the X(i,1) > X(i,2) & X(i+1,2) > X(i+1,1) is true. Is this intended?

Sign in to comment.

Answers (1)

Matt Tearle
Matt Tearle on 4 Feb 2013
The loops for z aren't going to achieve anything because phi and alpha are scalars, so all you're really doing is z = t_star1;
As far as I can tell what you're doing, it looks like you're solving an ODE each time with different values for phi and alpha, then calculating some (scalar?) value t_star1 from the solution, and I assume you want to see t_star1 as a function of phi and alpha. I also assume that phi and alpha are referenced in your ODE function n_v_w_ode, along with the other parameters that are listed as global. First, get rid of global! Rewrite n_v_w_ode to take as many inputs as needed, starting with t and y. Then:
phi = 0:0.1:1;
alpha = 0:0.1:1;
nphi = length(phi);
nalpha = length(alpha);
y0=[0.99, 0.01,0.0];
tin=[0 :10: 4000];
z = zeros(nphi,nalpha);
for j = 1:nphi
for k = 1:nalpha
% make a function handle to the ODE, with parameters embedded
f = @(t,y) n_v_w_ode(t,y,phi(j),alpha(k),r1,r2,...);
% solve ODEs
[t, y]=ode15s(f,tin,y0);
X = y(:,1:3);
% etc etc, get t_star1
z(j,k) = t_star1;
end
end
pcolor(z) % etc
All that stuff to get t_star1 could possibly be cleaned up, but I can't figure out the logic of it just by looking at it right now.
None of the stuff from ctr = ctr+1 to the end of the loop looks necessary to me.

Categories

Find more on Numerical Integration and Differential Equations 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!