Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Crank-Nicholson Scheme On Matlab (putting two

Subject: Crank-Nicholson Scheme On Matlab (putting two

From: Gemma Rose

Date: 5 Dec, 2006 09:51:10

Message: 1 of 2

Hey,

I'm really stuck on this Matlab question. It's Uni stuff which I've
almost completed save for one question and I reeeaaaalllly need you
guys' help on this thing.

Anyway, I've been given to Matlab functions, impheat and expheat:

impheat

function [u,tau]= impheat(ndiv,nstep,r)
%impheat implicit finite difference solution of 1-D heat equation
% [u,tau] = impheat(ndiv,nstep,r);
% input
% ndiv # of spatial subdivisions
% nstep # of time steps
% r implicitly defined timestep k=r/(ndiv*ndiv)
% output
% u solution vector
% tau final time
%
% Last modified 2 November 2006
% Copyright (c) 2006 D.J. Silvester
h=1/ndiv;
fprintf('subdivision parameter : %g\n',h)
x=h:h:1-h;
%%%% define and plot initial condition
u=min(2*x,2-2*x);
plot([0,x,1],[0,u,0],'-k',[0,x,1],[0,u,0],'or')
axis('square'), pause, hold on
%%%%
n=length(u); tau=0; k=r*h*h;
fprintf('timestep : %g\n',k)
%%%% loop over the timesteps
for step=1:nstep
    tau=tau+k;
    gamma=-r; alpha=1+2*r;beta=-r;
    u=trisolve(ndiv-1,gamma,alpha,beta,u);
    plot([0,x,1],[0,u',0],'-b')
    end
hold off
title(['Implicit FD Scheme: r is ',num2str(r)])
xlabel('x'), ylabel('temperature')
return

---------

expheat

function [u,tau]= expheat(ndiv,nstep,r)
%expheat explicit finite difference solution of 1-D heat equation
% [u,tau] = expheat(ndiv,nstep,r);
% input
% ndiv # of spatial subdivisions
% nstep # of time steps
% r implicitly defined timestep k=r/(ndiv*ndiv)
% output
% u solution vector
% tau final time
%
% Last modified 2 November 2006
% Copyright (c) 2006 D.J. Silvester
h=1/ndiv;
fprintf('subdivision parameter : %g\n',h)
x=h:h:1-h;
%%%% define and plot initial condition
u=min(2*x,2-2*x);
plot([0,x,1],[0,u,0],'-k',[0,x,1],[0,u,0],'or')
axis('square'), pause, hold on
%%%%
n=length(u); tau=0; k=r*h*h;
fprintf('timestep : %g\n',k)
%%%% loop over the timesteps
for step=1:nstep
    tau=tau+k;
    alpha=1-2*r;beta=r;
    v(1)=alpha*u(1)+beta*u(2);
    for i=2:n-1
        v(i)=beta*u(i-1)+alpha*u(i)+beta*u(i+1);
        end
    v(n)=beta*u(n-1)+alpha*u(n);
    u=v;
    plot([0,x,1],[0,u,0],'-b')
    end
hold off
title(['Explicit FD Scheme: r is ',num2str(r)])
xlabel('x'), ylabel('temperature')
return

----

and I'm supposed to combine these two functions to give an
implementation of the Crank-Nicolson scheme.

I've tried soooo hard to do it and am now just resting my head on my
table and crying.

If anybody can help me out I would greatly appreciate it.

Thank you! xxx

Subject: Crank-Nicholson Scheme On Matlab (putting two

From: Greg von Winckel

Date: 5 Dec, 2006 11:08:08

Message: 2 of 2

I won't do your homework for you, but I will give you a hint.

The Crank-Nicolson (no H) scheme is as follows

For a system of ODEs, u'=Au

u(:,n+1)=(I-dt*A/2)\((I+dt*A/2)*u(:,n));

separate this as

v=(I+dt*A/2)*u(:,n);
u(:,n+1)=(I-dt*A/2)\v;

This means that you take a half-sized explicit-time step and then a
half-sized implicit time step.

All you have to do is alternate between your implicit and explicit
function calls.

HTH,

Greg

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us