[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How to write function to plot?
From: |
Ben Abbott |
Subject: |
Re: How to write function to plot? |
Date: |
Sat, 25 Apr 2009 11:22:42 -0400 |
On Apr 25, 2009, at 10:52 AM, John B. Thoo wrote:
Hi, everyone. I have a question about writing a function for
plotting some data saved to a file.
I have some data saved to a file "heat_q.mat" for the variables k, x,
t, u. Here's a snippet.
%%%%%%%%%% begin snippet %%%%%%%%%%%
# Created by Octave 3.0.5, Fri Apr 24 13:11:38 2009 PDT <address@hidden
Thoos-Computer.local>
# name: k
# type: scalar
3
# name: x
# type: matrix
# rows: 1
# columns: 21
0 0.3141592653589793 0.6283185307179586 0.9424777960769379
1.256637061435917
...
# name: t
# type: range
# base, limit, increment
0 1 0.1
# name: u
# type: matrix
# rows: 21
# columns: 11
0 0 0 0 0 0 0 0 0 0 0
0.8090169943749475 0.3323236694342593 0.136510137653631
0.05607490346365374
...
%%%%%%%%%% end snippet %%%%%%%%%%%
If I load the file and then enter on the command line
l = 3;
plot (x, u(:,l), '-ok', x, exp (-k^2*t(l))*sin (k*x), '-b');
then I get a plot. However, if I use the following function
%%%%%%%%%%%%%%%%%%%%%%%
function plotheat_q (l)
% plot numerical solution and exact solution at time t0+l*dt;
plot (x, u(:,l), '-ok', x, exp (-k^2*t(l))*sin (k*x), '-b');
endfunction
%%%%%%%%%%%%%%%%%%%%%%%
then I get the following error:
octave-3.0.5:2> load heat_q.mat k x t u
octave-3.0.5:3> plotheat_q (3)
error: `x' undefined near line 4 column 7
error: evaluating argument list element number 1
error: called from `plotheat_q' in file `/Users/jbthoo/Desktop/
NumMethPractice/plotheat_q.m'
octave-3.0.5:3>
What am I doing wrongly?
Thanks.
---John.
The function has its own private variable space. You can fix this is
two ways
First, pass the variables to the function
%%%%%%%%%%%%%%%%%%%%%%%
function plotheat_q (l, k, t, x, u)
% plot numerical solution and exact solution at time t0+l*dt;
plot (x, u(:,l), '-ok', x, exp (-k^2*t(l))*sin (k*x), '-b');
endfunction
%%%%%%%%%%%%%%%%%%%%%%%
then
octave-3.0.5:2> load heat_q.mat k x t u
octave-3.0.5:3> plotheat_q (3, k, x, t, u)
Second, load the file inside the function
%%%%%%%%%%%%%%%%%%%%%%%
function plotheat_q (l)
load heat_q.mat k x t u
% plot numerical solution and exact solution at time t0+l*dt;
plot (x, u(:,l), '-ok', x, exp (-k^2*t(l))*sin (k*x), '-b');
endfunction
%%%%%%%%%%%%%%%%%%%%%%%
and then
octave-3.0.5:3> plotheat_q (3)
Ben