The Matlab function rand returns a uniformly distributed random number between 0 and 1. Uniformly distributed means it’s just as likely to return 0.002 as it is 0.928. The two most common usages are passing either one or two variables. If one variable, say N, is passed, Matlab will return an N x N array of random numbers. If two variables are passed, say N and M, it will return an array of size N x M.

Here’s an example:

>> rand(3)
ans =
 
   0.498945   0.088035   0.667322
   0.903201   0.554317   0.231287
   0.472007   0.432178   0.740403
 
>> rand(3,2)
ans =
 
   0.032335   0.626125
   0.610221   0.395778
   0.783036   0.953567

If you wanted a column of random numbers, you could simply assign x = rand(N,1), where N is the number of elements you want in your array (or column). Continue reading »

 

The MySQL command for selecting the maximum value from a column in a table is straightforward. First, here’s how you do it from a command line (or PHPmyAdmin) in MySQL.

For the following example, I have a database called weight_db and in that database a table called my_weight. There are several fields in this table, one if which is called wt and contains weight data. This is the column I want to select the maximum value from, using the max command.

mysql> use weight_db;
Database changed
mysql> SELECT max(wt) FROM my_weight;
+---------+
| max(wt) |
+---------+
|     199 |
+---------+
1 row in set (0.00 sec)

So, it’s simple and straightforward within MySQL. To do anything with this data in PHP is still fairly simple, it just requires another step. Continue reading »

 

The previous two posts showed you how to create a MySQL Database with PHP and how to create tables within that database. Now we’re going to create a simple HTML form that will call a simple PHP script to write data to this MySQL database. If you have a database with the tables you want and you’re ready to write to it, this example should give you a good start. If you still need to create the database and tables, check out the previous two tutorials:

How to create a MySQL database with PHP

How to create MySQL database tables with PHP

Let’s get started… we’ll continue with our Database of Clowns example. We’ve already created our database, called “bozo.” We’ve got one table in it, which we’ve called “famous_clowns.” This table has five columns called: seed, name, country, rating and comment. A quick description – seed is a simple integer that will get incremented each time we add a line to the table. This will get done automatically (the user doesn’t have to worry about it, and probably won’t know it’s even there.) The next two data fields are the clown’s name and his country of origin. Rating is just a number from 1-100 and comment is anything the user wants to say about this particular clown.

The following is a simple form that will capture this data: Continue reading »

 

The last tutorial, How to Create a MySQL Database with PHP, showed us how to create a database on the fly. This example will show us how to make the database usable by creating tables in it. The database we previously created was called “bozo.” Yes, as in Bozo the Clown. The first table we’re going to create will be called “famous_clowns.”

Assuming you did the last tutorial, or you have a database called “bozo,” you can run the following code to create a table in it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
< ?PHP 
 
mysql_connect("localhost", "fred", "fredpw") or die (mysql_error());
mysql_select_db("bozo");
 
$tablename = "famous_clowns";
 
$maketable="CREATE TABLE $tablename (
seed INT NOT NULL AUTO_INCREMENT,
name VARCHAR (30),
country VARCHAR (20),
rating INT (3),
comment VARCHAR (100),
PRIMARY KEY (seed)
);";
 
mysql_query($maketable) or die (mysql_error());
 
?>

Line 3 is the standard line for connecting to MySQL with my fictional user named Fred and his simple password: fredpw. Line 4 selects the database we’ll be creating the table in.

Line 6 just defines a variable we’ll use to name the table. In this case Continue reading »

 

As a programmer, I would typically use the MySQL control panel or PHPmyAdmin if I wanted to create a database. But occasionally you may have a need to create a database on the fly. This simple tutorial will show you how to create a database from within PHP.

First, MySQL is smart enough not to allow you to create a database if one by that name already exists. We can avoid that potential error by simply checking to see if one already exists. For demonstration purposes, we’ll use a simple if/else conditional statement.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
< ?PHP 
 
mysql_connect ("localhost", "fred", "fredpw") or die ('error: ' . mysql_error());
 
$new_db = "bozo"; // the name of our new database
 
$exists = mysql_select_db($new_db); // check to see if it exists
 
if ($exists) { // if database already exists
  echo ("<p>Database ". $new_db. " already exists.");
  } else {
  echo ("<p>Database " . $new_db . " does not exist.");
}
 
?></p>

As with a lot of my test scripts, Line 3 just connects to MySQL with a username and password. I run MySQL and PHP locally on my laptop which makes testing scripts much simpler. Fred is my test user and his password is “fredpw.” Yep, that’s original.

Line 5 defines a variable with the name of the new database we’re going Continue reading »

 

This tutorial will show you how to create a simple line graph in PHP. We’ll pick an example data set of daily high temperatures for the month of December. Any data that renders well in a line graph would suffice. You can see a live demo of the graph here.

This script could easily be altered to pull values from a dynamic MySQL database (or any database), but for this example we’ll simply define the data as a simple array of 31 temperature values (one for each day of the month).

The first 12 lines in the script simply create the array and define the final size of the image we want our chart to be. We also give it a title here.

1
2
3
4
5
6
7
8
9
10
11
12
< ?php
 
$temp= array(
35,32,29,31,37,36,31,32,31,33,
28,29,27,33,30,31,28,20,26,24,
28,26,27,25,26,23,25,24,27,22,23
);
 
// Define image size and properties.
$tot_width = 500;
$tot_height = 300;
$title = "December Daily Temperatures";

The next four lines assign a variable called “count” which just gives us a count of elements in the array. For example, if we were to pull these temperatures from a dynamic database in the middle of the month, we may only have 15 temperatures, and our count would reflect this.

The ymax and ymin variables are needed to help us set the scale on the y-axis.

14
15
16
17
18
$count = count($temp);
$max = max($temp);
 
$ymax = $max+3;
$ymin = $ymax-($tot_height/10);

Line 20 defines the margin in pixels we want our chart to have. We don't want to go Continue reading »

 

This tutorial will show you how to create a simple bar graph. Maybe you have fairly static data such as monthly revenues earned last year. In this case, your table may be two simple columns, one with the month and the other with a dollar value. You can see a live demo of the graph here.

In practice, if I needed a static graph such as this I would probably create it in a spreadsheet and simply save the chart as an image. But this is an exercise in creating a graph with PHP so this simple example should serve this purpose.

Also, this script could just as easily be altered to pull values from a dynamic MySQL database (or any database), but for this example we’ll simply define the data as a couple of simple arrays.

1
2
3
4
5
6
< ?php
 
$month = array('Jan','Feb','Mar','Apr','May','Jun',
                'Jul','Aug','Sep','Oct','Nov','Dec');
$revenue= array(21.55,24.26,28.88,25.11,26.74,24.79,
                 29.95,30.15,27.45,29.25,30.75,34.65);

Next, we’ll define the size we want the image to be and subtract the margins to define the actual size of the graph.

6
7
8
9
10
11
12
13
# Define the total size of the image
$img_width = 500;
$img_height = 300;
 
# Find the size of actual graph by subtracting the size of borders
$margins = 20;
$graph_width = $img_width - $margins * 2;
$graph_height = $img_height - $margins * 2;

Line 14 actually creates the image in PHP.

14
$img = imagecreate($img_width,$img_height);

Next, we'll define the width we want each bar to be and Continue reading »

 

The countries involved were primarily the United States, Great Britian and Canada. There were several prominent physicists involved in the project, most notably, the research was led by the American J. Robert Oppenheimer. The project was conducted between 1942 and 1946 in various (often secret) locations in the United States.

While Oppenheimer directed the research, it was a letter from Albert Einstein to President Roosevelt that really gave the project its roots in 1939. In this letter, Einstein expressed his concerns that Nazi Germany was trying to Continue reading »

© 2012 mattoneal.com Suffusion theme by Sayontan Sinha