# Matlab Program to Use Math Commands Assignment Solution.

## Instructions

Objective
Write a matlab program to use math commands in matlab.

## Requirements and Specifications

** Exercises Section 1 A
Before starting a new problem that uses the same variables, you want to clear the variables using the clear command, e.g. clear x y f or clear all.
1. Use the sqrt() command to find the values of: . Use plot() to plot the values of vs.
2. Use the abs() command to find the values of: . Use plot() to plot the values of vs.
3. Use both plot() and rand() to plot 1000 uniformly distributed random numbers in the interval as blue dots.
4. Use cosh() to find the values of: . Use both plot() and semilogy() to plot the values of vs. .
5. Use exp() to find the values of: . Use both plot() and semilogy() to plot the values of vs. .
6. Use both plot() and randn() to plot 1000 normally distributed random numbers, with a mean of zero and a standard deviation of 1, as red dots.
** Exercises Section 1 B
1. Calculate and plot the values for .
2. Calculate and plot the values for .
3. Calculate and plot the values for .
** Exercises Section 1 D
Use the @() command to build inline functions and solve the following problems. Use a script to enter your commands.
1. Evaluate the function and plot the on a scatter plot using blue asterisks.
2. Evaluate the function and plot the on a scatter plot using red points.
3. Evaluate the function and plot the at 100,001 points on a semilog plot.
** Exercises Section 2
Use the appropriate looping command to solve these problems.
• The following series converges. Estimate the limit of the series to 10 significant figures.
• Find the value of the number created using the following pattern
• Find the value of the number created using the following pattern
• Find the minimum number of terms in the following pattern such that the number is greater than .
** Exercises Section 3 A
To solve the following problems, use the if … elseif … end command in combination with loops and other functions as required.
1. Count the number of natural numbers from to 1e6 for which the following is satisfied
2. Find the natural numbers that satisfy the inequality in problem 1).
3. Evaluate the function . Find the values for which the function value is less than .
4. Count the number of integers between and 1000 for which the following is satisfied where.
** Exercises Section 3 B
To solve the following problems, use the if … elseif … end command in combination with loops and other functions as required.
1.  Generate a vector containing 100 uniformly distributed random numbers in , and build a vector containing all of the numbers in the interval . Hint: use the and() command to apply both conditions simultaneously.
2.  Bin the natural numbers from 1 to 1,000,000 according to their cosine function evaluation. Create 4 bins using the following rules:
Exactly how many numbers are in each bin?
1.  Find all of the numbers between 1 and 1000 that have either 23 or 29 as a factor.
2.  Find the first number as such that . Determine the value of to 12 decimal places of accuracy.
Source Code
```MATLAB 1 clear all close all clc %% Section 1A %% 1) x = 1:1:10; % from 1 to 10 sqrtx = sqrt(x); % square root of all values of x % Plot figure plot(x, sqrtx), grid on xlabel('x') ylabel('sqrt(x)') print -dpng Section1_1 %% 2) x = -5:1:5; % from -5 to 5 absx = abs(x); % Plot figure plot(x, absx), grid on xlabel('x') ylabel('abs(x)') print -dpng Section1_2 %% 3) % Generate 1000 random points x = rand(1, 1000); % Plot figure plot(x, 'b.') print -dpng Section1_3 %% 4) % Define x x = 1:100; % Plot figure subplot(1,2,1) plot(x, cosh(x)) grid on title('x vs. cosh(x)') subplot(1,2,2) semilogy(x, cosh(x)) grid on title('x vs. cosh(x) in semilog scale') print -dpng Section1_4 %% 5) x = 1:100; % values of x figure subplot(1,2,1) plot(x, exp(x)) grid on title('x vs. exp(x)') subplot(1,2,2) semilogy(x, exp(x)) grid on title('x vs. exp(x) in semilog scale') print -dpng Section1_5 %% 6) % Generate normally distributed random numbers x = randn([1,1000]); figure plot(x, 'r.') grid on print -dpng Section1_6 %% SECTION 1B %% 7) x = -5:5; figure plot(x, x.^3) xlabel('x') ylabel('{x^{3}}') grid on print -dpng Section1_7 %% 8) x = -5:5; figure plot(x, x.*sqrt(x)), grid on xlabel('x') ylabel('x*sqrt(x)') print -dpng Section1_8 %% 9) % Define values between -1 and 1 with a step of 0.1 x = -1:0.1:1; y = abs(cos(x).*exp(x)); figure plot(x, y), grid on xlabel('x') ylabel('|cos(x)exp(x)|') print -dpng Section1_9 %% SECTION 1D %% 10) % Define inline function f = @(x)(sqrt(x).*cos(3*x)); x = -5:5; y = f(x); figure scatter(x, y, 'marker', '*'), grid on print -dpng Section1_10 %% 11) % Define inline function f = @(x)((-x+sqrt(x.^2-25))./2); % Define x x = -12:12; y = f(x); figure scatter(x, y, 'marker', '.', 'MarkerEdgeColor', 'r', 'MarkerFaceColor', 'r'), grid on print -dpng Section1_11 %% 12) % Define inline f = @(x)(2*(36-x.^2)); % Define x x = -6:6; y = f(x); figure semilogy(x, y), grid on print -dpng Section1_12 MATLAB 2 clear all close all clc %% 1) % Define error err = Inf; % Define initial value of n n = 1; % Define tolerance tol = 1e-10; % 10 significant figures % Define initial value of the sum result = 0; while(err > tol)     old_value = result;     % Update result     result = result + 1/n^5;     % Calculate error     err = abs(old_value - result);     % Increment n     n = n + 1; end fprintf("The estimated limit of the series is: %.10f\n", result); %% 2) result = 0; for i = 1:499     result = result + i*(i+1)*(i+2); end fprintf("The number created is: %.4f\n", result); %% 3) base = 1; % initial base result = 0; for k = 0:0.1:2     %fprintf("%.0f^%.1f\n", base, 3.0-k);     result = result + base^(3.0-k);     base = base + 1; end fprintf("The number created is %.4f\n", result); %% 4) b = 0; % Define initial values for numerator and denominator of the terms numer = 4; denom = 1; n = 0; while(b <= exp(5)) % exit when b > exp(5)     b = b + numer/denom;     numer = numer+1;     denom = denom + 1;     n = n + 1; end fprintf("It was required %d terms such that b > exp(5)\n", n); MATLAB 3 close all clear all clc %% Section 3 %% 1) k = 0; numbers = []; % store the natural numbers in this vector for n = 1:1e6     if abs(cos(n)) < 1/n         numbers = [numbers, n];         k = k + 1;     end end fprintf("There are %d natural numbers such that |cos(n)|< 1/n\n", k); %% 2) fprintf("The numbers are: "); disp(numbers); %% 3) % Define x x = -50:50; % Define function as an inline function f = @(x)(sin(7*x)); numbers = []; % store numbers here for i = 1:length(x)     xi = x(i);     if f(xi) <-0.95         numbers = [numbers, xi];     end end fprintf("The values of x such that sin(7x) < -0.95 are:"); disp(numbers); %% 4) k = 0; for n = -1000:1000     if mod(n, pi) > 3         k = k + 1;     end end fprintf("There are %d numbers such that mod(n, pi) > 3\n", k); %% SECTION 3B %% 5) % Vector of 100 numbers x = rand(1, 100); % Now get all values between 0.6 and 0.7 y = []; % store values here for i = 1:length(x)     xi = x(i);     if and(xi>=0.6, xi <= 0.7)         y = [y, xi];     end end fprintf("The numbers between [0.6,0.7] are:\n") disp(y) %% 6) binA = []; binB = []; binC = []; binD = []; for n = 1:1e6     if cos(n) > 0.5         binA(length(binA)+1) = n;     elseif and(cos(n)>0,cos(n)<0.5)         binB(length(binB)+1) = n;     elseif and(cos(n)>-0.5,cos(n)<0)         binC(length(binC)+1) = n;     elseif cos(n)<-0.5         binD(length(binD)+1) = n;     end end fprintf("There are %d elements in bin A\n", length(binA)); fprintf("There are %d elements in bin B\n", length(binB)); fprintf("There are %d elements in bin C\n", length(binC)); fprintf("There are %d elements in bin D\n", length(binD)); %% 7) numbers = []; for n = 1:1000     if mod(n,23) == 0 || mod(n,29) == 0         numbers(length(numbers)+1) = n;     end end fprintf("The numbers that have 23 or 29 as factor are:\n"), disp(numbers); %% 8) % Use bisection method to solve for x = cos(x) a = 0; b = pi; err = Inf; % initial error tol = 1e-12; % up to 12 decimals f = @(x)(x-cos(x)); n = 1; while(err >= tol)     c = (a+b)/2;     if f(c) == 0 || (b-a)/2 < tol         % Solution found         break;     end     n = n + 1;     if f(c)*f(a) > 0         a = c;     else         b = c;     end end x = c; % solution found using bisection method % We solved for x = cos(x) % Now, since we want x > cos(x), we increment x by 1e-12 x = x + 1e-12; fprintf("The first value such that x > cos(x) is x = %.12f\n", x); % fprintf("The first value such that x > cos(x) is x = %.12f\n", x);```