Applying new data into a repeat of used function.

1 view (last 30 days)
Hi
I have a problem in that I've used ODE45 to solve for values, in this case, u to get a matrix from a finite difference approximation. Now I have to use the first row of those values and apply it to the FDA again albeit slightly different function and back into ODE45. I'm getting errors such as matrix dimensions must agree but basically what I want is for each FDA step to recall a constant value from before to use to get a new set of values with changed IC and a contribution from old values using ODE45.
Any help would be great!
Kevin
function testfile2
global n dx dt tau Xend u uh v
n=20; m=10; Xend=40; Tend=10; dx=Xend/n; dt=Tend/m;
tau=3; tspan=0:dt:Tend;
IC=f();
IC2=u(2,:);
[t,u]=ode45(@rhs,tspan,IC);
uh=u;
i=1:n;
v(i)=u(2,i);
uh(2:m,:)=uh(1:m-1,:);
uh(1,:)=u(2,:);
[t,u]=ode45(@rhs2,tspan,IC2);
uh(2:m,:)=uh(1:m-1,:);
uh(1,:)=u(2,:);
%[t,u3]=ode45(@rhs,tspan,IC3)
%Set up IC
function u0=f()
global n dx Xend tau
i=1:n+1;
x=(i-1)*(dx/Xend);
a=[x(x<0.5)*2, 2*(1-x(x>=0.5))];
a(1,1:tau)=1;
a(1,tau+1:n+1)=0;
u0=a;
%function unext1=f1()
%global uh
%a = uh(1,:);
%unext1=a;
%function unext2=f2()
%global u
%a = u(2,:);
%unext2=a;
%Finite difference approximation
function udot=rhs(t,u)
global n dx
y(1)=0;
y(n+1)=0;
j=2:n;
y(j)=(1/(dx)^2)*(u(j-1)-2*(u(j))+u(j+1)) + u(j).*(1 - u(j));
udot=y';
%FDA2
function udot2=rhs2(t,u)
global n dx v;
y(1)=0;
y(n+1)=0;
j=2:n;
y(j)=(1/(dx)^2)*(u(j-1)-2*(u(j))+u(j+1)) + (1 - u(j)).*v(j);
udot2=y';

Answers (0)

Categories

Find more on Creating and Concatenating Matrices 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!