Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % script for excercise 1 of case study 1 for EMATH171
- % in this script we solve for V using newtons method repetating untill we
- % have a tolerence of 0.0001
- clear
- clc
- close all
- % Coefficients for the function and its deriviative
- m = 1000;
- g = 9.81;
- theta = 0.25 * pi;
- coff_friction = 0.2;
- a = 1;
- b = 100 * pi;
- rgb = 20;
- R = 0.5;
- % functions
- j = @(v) (m .* g .* (sin(theta)) .* v) + (coff_friction .* m .* g .* (cos(theta)) .* v);
- % motor shaft rotation speed
- k = @(v) (a .* ((b .* ((rgb .* v) / R)) - (((rgb.^2) * (v.^2))/(R.^2))));
- % power output
- f = @(v) j(v) - k(v);
- % origional function
- g = @(v) (m .* g .* (sin(theta))) + (coff_friction .* m .* g .* (cos(theta))) - (a .* ((b .* (rgb / R)) - ((2 .* (rgb.^2).* v)/(R.^2))));
- % deriviative
- h = @(old_x) old_x - ((f(old_x))/(g(old_x)));
- % newtons method propper
- % Coefficients for newtons method
- N = 200;
- % maximuim amount of repetitions, unlikley to actualy reach this value
- x = 2;
- % initial guess
- tol = 0.0001;
- %tolerence of diffrence between iterations
- out_array = [x];
- % output array of the results of newtons methods approximations
- error_array = [abs(f(x))];
- % array of the error
- delta_array = [];
- % array of change between iterations
- counter = 0;
- % tracks number of iterations
- % Newtons method implementaion
- for item = 1:N
- old_x = x; % saving old x
- delta_array(item) = (x);
- x = h(x); % creating new x
- counter = counter + 1;
- out_array(item+1) = x;
- error_array(item+1) = abs(f(x));
- delta_array(item) = abs(delta_array(item)- x);
- if and((abs(f(x))<tol),(abs(x-old_x)<tol))
- counter = counter - 1;
- break
- end
- end
- % display
- figure
- hold on
- x_actual_array = [0:0.1:5];
- plot (x_actual_array, j(x_actual_array))
- plot (x_actual_array, k(x_actual_array))
- hold off
- x
- counter
- out_array
- error_array;
- delta_array;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement