Physics 281 - Computational Physics

Wednesday/Friday Section

Fall 2009

Exercise 4 - Practice with MATLAB Functions

Practice:

1. Write a very simple main script - function script pair where the main script calls the function to do something. For example, see the following:

Example Main Script

z = input('enter a number: ');
x = my_function(z);
fprintf('the function returns %f\n',x)

Example Function Script: "my_function.m"

function result = my_function(number)
fprintf('the number passed to the function is: %f\n',number);
result = 2*number;

2. Implement the target.m script and its function cannon_ball_distance.m and play the "target" game.

Assignment:

Modify the program written for exercise 3 so that the equation

is evaluated by a MATLAB function for a given value ot "t". Your function should take as arguments: (1) the time; (2) the initial amplitude; (3) the gravitational acceleration; and (4) the length of the pendulum. It should return the amplitude of the pendulum.

The name of the program file should be something like exercise4.m so that it can be distinguished from other submissions. You can name the function file anything you like provided that the name is easy to identify with this exercise. For example "pendulum_eqn.m" might be a good choice.

Please write your name in a comment field near the beginning of the script.

Copy your completed program into the directory ~schloerb/ph281/username, where username is your user name. See this link for examples of how to copy a file to this directory.

Go to Ph281 Home

Go to Ph281 Topics