## Transcribed Text

Programming fundamentals
1. Write a Matlab™ program to calculate the sine of an angle from its series. For an angle of 5
radians and using 10, 15 and 25 terms in the series, compare this to values produced by the sine
(i.e. sin()) function provided in Matlab™. Use 15 digits of precision when comparing these two
numbers. Put comments in your program so that the operation is readily understood.
The series for computing the sine of a value is
Matlab functions and features
2. State how a compiler or interpreter such as Matlab™ distinguishes between a function and a
variable.
3. Indicate which of the following variable names are not legal variable names in Matlab, and
explain why. Indicate, with reasons, which ones are legal but not recommended.
(i) A65 (ii) a6b (iii) spectra91 (iv) exit (v) 78Si
(v1) High Five (vii) price$ (vii) my()book (ix) pi (x) a_5
(xi) cos30 (xii) sin (xiii) pi3 (xiv) Pi (xv) 7_v
4. Illustrate with a short code how the ‘switch’ structure might be replaced by ‘if’ statements.
5. Determine the data types of the parameters c and C in the following
a)
>> a=’monday’;
>>c=2*a;
b)
>> a=[1 9 6
4 7 3
7 5 9]
>>b=[23 57 82
67 15 98]
>>c=a*b;
>>C=a*b’;
c)
>>t=0:0.01:2.5;
>>c=cos(t);
6. Write an anonymous function that computes the equation for the stiffness, , of a prismatic
cantilever beam of rectangular cross section
Where is the Young’s modulus, is the thickness, is the breadth (or width) and is the
length of the beam.
Generate a matlab program that uses this anonymous function to compute the stiffness of an
aluminum cantilever of length 250 mm, width 25 mm for thickness values ranging from1 to 4 mm in 100
steps. Plot this as a graph with labels.

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

Programming fundamentals

Problem 1

%The format to use

format long

%The number of terms to use

Number_of_terms=[10 15 25];

%The angle to use

x=5.0; %radians

%First display the answer using the Matlab function sin:

%display_string: the string which we use to display the answer

display_string=['The value of sin(' num2str(x) '):' num2str(sin(x),15)];

%display it

disp(display_string);

%It should display: The value of sin(5):-0.958924274663138

for ind=1:length(Number_of_terms)

%the approximation is as a series, initialize the first term to be zero

%and further add to it.

approximate_sin_value=0;

%The number of terms at this time

N=Number_of_terms(ind);

%Construct the values in the series and add to the

%approximate_sin_value

for s=1:N

to_add=(-1)^(s-1)/factorial(2*s-1)*x^(2*s-1);

approximate_sin_value=approximate_sin_value+to_add;

end

%At the end of this loop all the terms have been added.

%Diplay the result:

display_string=['The approximate value with ' num2str(N) ' terms: ‘ num2str(approximate_sin_value,15)];

disp(display_string);

end

%Output from the program should be:

% The value of sin(5):-0.958924274663138...