Hi Phil, Thank you for your tutorial. I have copied the transcript into my Matlab series 2014, but the notification told me "Unexpected MATLAB expression" in line 26, column 16. Would you give me some explanation for this problem ? Thx in advance
Hey there, can you cooy the line that‘s giving you troubles into thos chat? There‘s a good chance that since you are using an older version than I am, that some function I used was not available in 2014 version and that is why it‘s throwing an error.
This was a really interesting problem. Do you have any application examples where you used fractal geometry? Also, cool to learn about the switch function! Keep up the great work, congrats on OVER 1k subscribers!
Hi Alexa, fractal geometry is quite the interesting field. The only applications I know of are in biology. Check out this research paper pubmed.ncbi.nlm.nih.gov/32634372/ which discusses fractal geometry's interface with modeling branched components of the lung. In general, biology and nature (often referred to as having "no straight lines" - whether true or not) appear to developed in branched, fractal-like ways potentially not expressible by traditional mathematics. And thank you for the 1K subscriber love!
Hi Phil, you have given a great explanation! I'm currently using fractals as part of my honours project at the University of Adelaide and was wondering if this could be applied for making square Sierpinski Carpet instead of the triangles by simply just adding an extra point and modifying? Could this be modified to create circles even? If the explanation is long, I am happy to meet through online means! Cheers again!
Hi Kay, the code could easily be modified to do a square, pentagon, or other polygons! You would need to change the number of points to n (e.g., n = 4 for square), and then when you generate the random number that picks which virtex to move towards be sure it goes from 1 to 4! There may be other little tweaks but if you understand the code for the triangle, you should be able to update for any polygon (what’d be cool too is if the script was flexible to do so!). You mentioned extra help - I provide my Patrons email support if that is something you’re interested in! Patreon.com/PhilsBeginnerCode
Hi Phil really liked your tutorials on MATLAB. I also tried to do it for a square would love if you could check it out, it didn't came out exactly as I was expecting. Also how would you say the code should be modified to make any polygon (other than the addition of end points and center) Thanks for all the tutorials cheers and have a good day. clc, clearvars, close all, format compact % Input Parameters side = 10; % base of square n = 50000; % numbers of iterations init_pos = [0,0]; % x,y [initial position] % Starting Calcs pt1 = [0, 0]; % origin pt2 = [side, 0]; % x axis limit pt3 = [0,side]; % y axis limit pt4 = [10,10]; % top most corner pt5 = [side/2, side/2]; % starting mid point current_pos = init_pos; % For Loop for i = 1:n % pick random vertex event = randi(5); % calc midpoints based on vertex, switch statement switch event case 1 current_pos = calc_midpt(current_pos, pt1); case 2 current_pos = calc_midpt(current_pos, pt2); case 3 current_pos = calc_midpt(current_pos, pt3); case 4 current_pos = calc_midpt(current_pos, pt4); case 5 current_pos = calc_midpt(current_pos, pt5); end % save the values all_pos(i,1) = current_pos(1); all_pos(i,2) = current_pos(2); end % plot plot(all_pos(:,1), all_pos(:,2), '.r') hold on plot([pt1(1), pt2(1), pt3(1), pt4(1), pt5(1)] , [pt1(2), pt2(2), pt3(2), pt4(2), pt5(2)], 'bo', ... 'MarkerFaceColor', 'y', 'MarkerSize', 8) xlim([0 10]) ylim([0 10]) % midpoint function function [midpt] = calc_midpt(current_pos, target_pos) midpt(1) = (0.7*current_pos(1) + 1.3*target_pos(1))/2; midpt(2) = (0.7*current_pos(2) + 1.3*target_pos(2))/2; end
Hi there, after looking at your code (and developing a video on this), I think your only mis-step was that you including that 5th point (the center of the figure) as one of the random vertices you can select. It still generated a cool pattern, but not quite the squares you were hoping for. Here's the updated code that worked on my end: clc, clearvars, close all, format compact % Input Parameters side = 10; % base of square n = 5000; % numbers of iterations init_pos = [0,0]; % x,y [initial position] % Starting Calcs pt1 = [0, 0]; % origin pt2 = [side, 0]; % x axis limit pt3 = [0,side]; % y axis limit pt4 = [10,10]; % top most corner pt5 = [side/2, side/2]; % starting mid point current_pos = init_pos; % For Loop for i = 1:n % pick random vertex event = randi(4); % calc midpoints based on vertex, switch statement switch event case 1 current_pos = calc_midpt(current_pos, pt1); case 2 current_pos = calc_midpt(current_pos, pt2); case 3 current_pos = calc_midpt(current_pos, pt3); case 4 current_pos = calc_midpt(current_pos, pt4); %case 5 % current_pos = calc_midpt(current_pos, pt5); end % save the values all_pos(i,1) = current_pos(1); all_pos(i,2) = current_pos(2); end % plot plot(all_pos(:,1), all_pos(:,2), '.r') hold on plot([pt1(1), pt2(1), pt3(1), pt4(1), pt5(1)] , [pt1(2), pt2(2), pt3(2), pt4(2), pt5(2)], 'bo', ... 'MarkerFaceColor', 'y', 'MarkerSize', 8) xlim([0 10]) ylim([0 10]) % midpoint function function [midpt] = calc_midpt(current_pos, target_pos) midpt(1) = (0.7*current_pos(1) + 1.3*target_pos(1))/2; midpt(2) = (0.7*current_pos(2) + 1.3*target_pos(2))/2; end