By Carey A. Smith
Structure for simulation of the effect of an earthquake
% The magnitude 6.7 Northridge, California, earthquake of 17 January 1994
Earthquake.name = 'Northridge';
Earthquake.year = 1994;
Earthquake.mag = 6.7;
Earthquake.length = 15; % km
Earthquake.vertical = 70; % cm max vertical movement
Earthquake.duration = 20; % seconds
Earthquake.accel_g =1.82; % g
Earthquake.accel_ms2=Earthquake.accel_g*9.81; % m/s^2
% Simulation parameter
Earthquake.scale = 1.4; % Scale factor for simulations
Add example text here.
- Create a struct for a team of 5 basketball players. They may be men or women, real or fictional. For each member of the team create struct called "player".
- Create the following fields in the "player" structure for each player.:
- Name (a string)
- Position (Choose one of these ‘Center’, ‘Power Forward’, ‘Small Forward’, ‘Shooting Guard’, ‘Point Guard’)
- Height in inches (not feet) Example: 75
- Shirt number
- Fill in you teams structure array with a separate sub-structure for each of the 5 starting players.
- Example. Suppose you had already filled out the structures for the 1st 4 players. Then the 5th substructure could be defined as follows:
player(5).Name = 'Kareem';
player(5).Height = 84;
player(5).Shirt = 33;
- Create a “for loop” that displays each player’s information on one line. Each field should be separated by a tab “\t” or by 4 spaces. Use \n at the end of the format string to put a new line after each player's info.
fprintf(' Name Position Height Shirt\n') % \n creates a "new line"
fprintf('%13s \t%14s \t%i \t %i\n', ... % \t inserts a tab
player(n).Name, player(n).Position, ...
Create your own struct.
This example creates a triangle struct and uses it to draw 2 right triangles on one figure.
The first routine sets the parameters of each triangle.
The second file is a function that draws a triangle using the parameters in the struct. Only one argument, the struct, is passed to the function. Thus, a struct can group related variables and simplify function calls.
%% Triangle structure demo
clear all, format compact, format shortg; close all; fclose all; clc;
% 1st triangle
Tri1.pos = [-9,-1];
Tri1.width = 18;
Tri1.color = 'b';
%% Double-click on a structure in the Workspace to see the details
%% 2nd triangle
Tri2.pos = [0,4];
Tri2.width = 8;
Tri2.color = 'r';
function  = draw_triangle(Tri)
% Draw a right triangle
x = zeros(1,4); % Need 4 points (1st & last are the same)
y = x;
% 1st point
x(1) = Tri.pos(1);
y(1) = Tri.pos(2);
% 2nd point: to the right, with the same y as point 1
x(2) = x(1) + Tri.width;
y(2) = y(1);
% 3rd point: up, with the same x as point 1
x(3) = x(1);
y(3) = y(1) + Tri.height;
% Last point = 1st point
x(4) = x(1);
y(4) = y(1);
x = x
y = y
This is the resulting plot:
The logic and code for this exercise is similar to the
1. [5 pts] Create 3 rectangle structures that specify the position, size, and color of 3 rectangles with these characteristics:
% Rectangle 1
rect1.pos= [30,20]; % x and y coordinates of the lower left corner
rect1.width = 500;
rect1.color = 'b';
% Rectangle 2
rect2.pos= [350,300]; % x and y coordinates of the lower left corner
rect2.width = 220;
rect2.color = 'r';
% Rectangle 3
rect3.pos= [300,250]; % x and y coordinates of the lower left corner
rect3.width = 150;
rect3.color = 'c';
2. [7 pts] Write a draw_rectangle(rect) function whose uses a rect structure to plot 1 rectangle. Since variables in a function are local, you can use "rect" for the structure name inside the function. You don't need to use rect1, rect2, rect3.
3. [3 pts] Open a figure, use ”hold on;” and plot these 3 rectangles by calling your rectangle plotting function with each of the 3 rectangle structures.
- Like the draw_triangle function, the draw rectangle function needs to create an x vector & a y vector with the coordinates of the corners. In order to get back to the starting point & close the rectangle, you will need to have 5 pairs of (x,y) coordinates, with the last coordinate being the same as the first coordinate.
- The 1st corner is like home plate. It is in the lower-left corner.
- The 2nd corner is like 1st base. It’s x-coordinate is the 1st corner’s x-coordinate + the width of the rectangle. It’s y-coordinate is the same as the 1st corner’s .
- The 3rd corner is like 2nd base. It’s x-coordinate is the same as the 2nd corner. It’s y-coordinate is 2nd corner’s y-coordinate + the height of the rectangle.Figure out the coordinates of 3rd base. The last line goes from 3rd base to home plate.
- To plot each of the 4 lines, the 1st argument of plot() is a vector of the X coordinates.The 2nd argument of plot() is a vector of the Y coordinates.
The resulting plot should look like this:
Add texts here. Do not delete this text first.