0% found this document useful (0 votes)
89 views

Matlab Tutorials

This document contains 3 MATLAB tutorials related to polynomials and root finding: 1. The first tutorial defines a function to test the uniform distribution of a user-supplied array by calculating the percentage of values below a given fraction. 2. The second tutorial defines a function to calculate the root of a user-defined polynomial nearest a guess value using the Newton-Raphson method. 3. The third tutorial uses a while loop to prompt the user to repeatedly enter polynomials of different orders to build up a dataset.

Uploaded by

Ali Gh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views

Matlab Tutorials

This document contains 3 MATLAB tutorials related to polynomials and root finding: 1. The first tutorial defines a function to test the uniform distribution of a user-supplied array by calculating the percentage of values below a given fraction. 2. The second tutorial defines a function to calculate the root of a user-defined polynomial nearest a guess value using the Newton-Raphson method. 3. The third tutorial uses a while loop to prompt the user to repeatedly enter polynomials of different orders to build up a dataset.

Uploaded by

Ali Gh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Tutorial 5

%RT Paton
%2010.02.22
%MatLab1.m
%Function used to determine the 'correctness' of the uniform distribution
%of a user-supplied array
%Inputs:
perc - fraction of 1 representing the percentage to be tested
%
nums - array of numbers to be tested
%Outputs:
Screen output of the actual fraction of the supplied array with
%
values below perc
function MatLab1(perc,nums)
numtrue = 0;
%Initialisation of variable used to count number of
elements of nums below perc
for q =1:1:length(nums)
%Loops through nums until the last value
if nums(q) < perc
numtrue = numtrue + 1; %Increases numtrue if the current array
value is lower than the supplied percentage
end
end
trueperc = 100*numtrue/length(nums);
%Calculates the proportion of the
array meeting the supplied percentage criterion as a percentage
['The true fraction of array less than ' num2str(100*perc) '% is '
num2str(trueperc) '%.'] %Creates a sentence showing the result to the user
that is unsuppressed i.e. prints to the screen

Tutorial 6
%RT Paton
%2010.02.22
%MatLab2.m
%Function used to calculate the root nearest a user-defined guess value of
%a user-defined polynomial using the Newton-Raphson method
%Inputs:
porder - order of polynomial defined via GUI
%
polyu - defined via GUI based on porder
%
guess - user guess of the root value (via GUI)
%Outputs:
Root nearest to user-supplied guess value
function MatLab2
diff = 1;
%Initialises the difference variable to be used later when
calculating the polynomial root
porderc = inputdlg('Please specify the order of the polynomial you wish to
examine');
%Uses a GUI to capture user information regarding the
polynomial order
porder = str2num(porderc{1});
%Converts the string of the user-efined
polynomial order to a number after retrieving it from the cell array
resulting from the use of the inputdlg
for q =1:1:porder+1 %One more than the specified order to ensure that the
constant co-efficent is also spcified
coeffc{q}= strcat('a_', num2str(porder+1-q));
%mathematics of num2str
ensures that co-efficient input listed in descending order as per MatLab
convention for polynomials
end
polyuc = inputdlg(coeffc,'Please specify the polynomial co-efficients');
%Input dialogue using the title bar to issue the instruction to the user
%Convert input cell of co-efficients into a MatLab-format polynomial array
for q = 1:1:length(polyuc)
polyu(q) = str2num(polyuc{q});
end
%Calculate the derivative polynomial
powcoeffs = porder:-1:0;
%Produces an array of multipliers based on the
existing powers of each saved co-efficient
polyudt = powcoeffs.*polyu; %Uses array-wise multiplication to calculate
the derivative polynomial coefficients
polyud = polyudt(1:end-1);
%Truncates the last value of polyudt otherwise
MatLab will calculate polyud as being one order higher than it should be
guessc = inputdlg('Please guess the value of the root of the polynomial');
%Accepts user guess of polynomial root
guess =str2num(guessc{1});
while diff > 1e-06 %Continues calculation until the difference from one
iteration to the next is lessthan 10^-6
guesst = guess -(polyval(polyu,guess))./(polyval(polyud,guess));
%Calculates the new root estimate
diff = abs(guesst - guess);
%Calculates the absolute value of the
difference from one step to the next
guess = guesst;
%Updates the value of the root estimate
end

msgbox(['The root of the specified polynomial nearest the specified guess


value is ' num2str(guess)]);
%Displays the calculated root to the user

MATLAB Tutorial Looping


function Data = Polyin
loop1 = 'Yes';
counter = 1;
while strcmpi(loop1,'No') ~= 1
ordcell = inputdlg('Please specify the order of the polynomial');
Data(counter).ord = str2num(ordcell{1});
loop1 = questdlg('Do you want to enter another polynomial?');
counter = counter + 1
end

You might also like