# matlab代写:COMP 208 Computers in Engineering Fall, 2018(附标准答案)

matlab代写: 大一典型的matlab代写题目,附标准答案
COMP 208: Computers in Engineering Fall, 2018
Assignment 5 The Mouse Chase
These assignments are to be done individually. You can collaborate on understanding the problem but you must write the solution individually. Your submission might be subject to Plagiarism detection software.
Due Date
Assignment 5 is due on November 20, 2018 at exactly 11:59. The cutoff is automated and is exactly at this time. Assignments submitted within the next hour will be considered late. After that time they will not be accepted at all.
Introduction
This assignment deals with a problem known as the mouse or beetle problem. A description can be found on the web site: http://mathworld.wolfram.com/MiceProblem.html
In the general problem, n mice start at the corners of a regular n-gon. Each mouse travels towards its closest neighbor (in a counterclockwise direction) at a constant speed. The paths that the mice take form spiral curves. An animation of the pattern can be seen on the above web site. The patterns formed for n=3, 4, 5 and 6 look like the following:

Assignment
In this assignment, you are to generate a numerical solution that determines the path of the mice.
Write a Matlab function definition that, given the value n, returns two vectors of length n representing the x and y coordinates of the n-gon that you start with.
Write a Matlab script that inputs a value n and generates the coordinates of each of the n mice as they move towards each other. You will then graph the paths the mice take.
Methodology
Start by initializing the positions of the n mice. The positions should be symmetric and they should all lie on the unit circle centered at the origin.
You could use two vectors of length n to represent the x- and y- coordinates of each of the mice.
On each iteration
 Each mouse should move a distance d towards its neighboring mouse. That is, towards the closest mouse in a counterclockwise direction. (Hint: To find the coordinates of neighbor of mouse x, you just have to add 1 to x modulo n. You can use the Matlab ‘mod’ function)
 Generate the new coordinates of each mouse.
In order the graph the function, you could store the sequence of vectors in matrices, with each line representing the coordinates at one of the time steps.
The iterations should be repeated until the mice are sufficiently close to each other.
You can use a value of 0.01 for d, the distance each mouse moves at each time step.
Test you program for at least 5 different values of n.

Requirements
Your code must meet these requirements:
 The script must be written in Matlab
 Use sensible variable names.
 Comment and indent your code
 Submit a text file with the scripts and function definitions you created.
Also submit the graphs you produced. Name your files A5_123456789 and Graphs_123456789 where 123456789 is your ID. The Graph file should have images of all the graphs generated for the different values of n.
If any of the above requirements is not respected you might lose marks.

% input n and calculate the n-gon
function [x, y] = calc_ngon(n)
% generate xs and ys initially fill zero
x = zeros(1, n);
y = zeros(1, n);
% calculate the degree of each point
theta = 2 * pi / n;
for i=1:n
deg = i * theta;
x(i) = cos(deg);
y(i) = sin(deg);
end

end

clear all; clc;
close all;
n = input(‘Enter the number of mice: ‘);
file1.m:
% get the n-gon
[x, y] = calc_ngon(n);

figure(1);
plot([x x(1)], [y y(1)]);
title(“mice”);
xlim([-1 1]);
ylim([-1 1]);

d = 0.01;

file2.m:

% when two mice are close enough, terminal the while loop
finish_delta = 1e-1;

while true
% the new x of the mice
new_x = zeros(1, n);
% the new y of the mice
new_y = zeros(1, n);
for i=1:n
mice_x = x(i);
mice_y = y(i);
neigh_idx = mod(i, n)+1;
neigh_x = x(neigh_idx);
neigh_y = y(neigh_idx);

``````    line_vec = [(neigh_x-mice_x) (neigh_y-mice_y)];
line_distance = sqrt(line_vec * line_vec');
% calculate the new_x new_y of each mice
new_x(i) = mice_x + d/line_distance*line_vec(1);
new_y(i) = mice_y + d/line_distance*line_vec(2);
end
x = new_x;
y = new_y;
hold on;
scatter(x, y, 5);
% line between first and second mice
line_vec = [(x(2)-x(1)) (y(2)-y(1))];
distance = sqrt(line_vec * line_vec');

if distance < finish_delta
break;
end
pause(0.1);
``````

end