Part 1: Simple draw function The first step in coding up a MATLAB function that
ID: 654915 • Letter: P
Question
Part 1: Simple draw function
The first step in coding up a MATLAB function that draws the fractal tree is to write a function that draws a vertical line segment (the trunk of the tree) with its base at the origin. You may choose whatever length you want. Then, your function should draw two line segments at right angles from the top of the original line segment, each 0.6 times as long as the original:
Your function should take two arguments ? the endpoints of the original line segment and a number of levels of the tree to draw ? and to work properly regardless of where those endpoints are. This will mean using the rotation matrix math from Lab 7 to rotate the segment, and a scalar matrix multiplication to change its length. Remember that the rotation matrix will rotate the whole line segment around the origin. If you just want to rotate it around one of its ends, you first move it so that that end is at the origin, do the rotation, then move it back. Don?t forget to move both ends of the line segment whenever you move one end. Your function should add the two new branches to the drawing and return their endpoints.
Part 2: First Right Branch, then Left branch. Repeat in a loop
Now you?re set up to draw a fractal tree. What remains is writing a new function that uses a ?recursion? loop to repeat the operation that your last function performed: The thinking here goes like this: draw the original line segment, then call a function (draw-rightbranch, say) that takes that segment and computes & draws the right branch, and then call another function (draw-left-branch) that takes a segment and computes & draws the left branch. Here?s the tricky part: the right branch of the fractal has a left & right branch, each of which has right & left branches, and so on. (That?s part of what it means to be a fractal.) This means that draw-right-branch has to call draw-left-branch and draw-right-branch. (So does draw-left-branch.) Recursion is hard to grasp when you first encounter it.
Part 3: Add more parameters
Once you have this working, adapt your program to take several new arguments that specify the length ratios and angles between each branch & the trunk separately (i.e., so that the geometry of the two sides of each new set of branches can be varied independently). Try 0.7 and 0.65 for the length ratios, for instance, and 60 & 40 degrees for the angles. Experiment with some other angles & length ratios and turn in a couple of interesting plots. You should be able to make ferns, trees, and lots of other structures.
Explanation / Answer
1. Simple draw function
% Load the step response data
load stepResponse step_data
% Plot the step response
figure plot(step_data(:,1), step_data(:,2), 'r*-')
% Set the axis limits axis([0 200 0 1.2])
% Add a title and axis labels
title('Closed loop response'xlabel('Time')
ylabel('Tank temperature (normalized)')
% Add a horizontal line for the Temperature at steady state
line('XData', [0 200], 'YData', [1 1], 'LineStyle', '-', ... 'LineWidth', 2, 'Color','m')
% Add lines for the temperature at time = 25
x = 25; y = (step_data(4,2) - step_data(3,2));
line('XData', [x x 0], 'YData', [0 y y], 'LineWidth', 2, ... 'LineStyle', '-.', 'Color', [0 0.6 0])
% Add lines for the temperature at time = 60 x = 50; y = step_data(6,2);
line('XData', [x x 0], 'YData', [0 y y], 'LineWidth', 2, ... 'LineStyle', '-.', 'Color', [0.2 0.4 1.0])
2. For add parameters
suppose you want to minimize the function
for different values of a, b, and c. Solvers accept objective functions that depend only on a single variable (x in this case). The following sections show how to provide the additional parameters a, b, and c. The solutions are for parameter values a = 4, b = 2.1, and c = 4 near x0 = [0.5 0.5] using fminunc.
Anonymous Functions
To pass parameters using anonymous functions:
Write a file containing the following code:
function y = parameterfun(x,a,b,c) y = (a - b*x(1)^2 + x(1)^4/3)*x(1)^2 + x(1)*x(2) + ... (-c + c*x(2)^2)*x(2)^2;
Assign values to the parameters and define a function handle f to an anonymous function by entering the following commands at the MATLAB
f(x)=(a?bx21+x41/3)x21+x1x2+(?c+cx22)x22 (