pi Estimation The value of pi can be estimated from the expr

pi Estimation The value of pi can be estimated from the expression: 2/pi = Squareroot2/2. Squareroot2 + Squareroot2/2. Squareroot2 + Squareroot + Squareroot2/2.... In a script file named YourLastName_P1.m, write a MATLAB program that determine pi for any number of terms. The program asks the user to enter the number of terms, and then calculates the corresponding value of pi. Test your program with 5,10, and 40 terms. Compare the result with pi. (Use format long) 2. Sorting Elements A vector is given by x = [4.5 5 - 16.12 21.8 10.1 10 - 16.11 5 14 - 3 3 2]. In a script file named YourLastName_P2.m, using conditional statements and loops, write a program that rearranges the elements of x in order from the smallest to the largest. Do NOT use MATLAB built-in function sort. 3. Finding n In a script file named YourLastNome_P3.m, write a MATLAB program that finds a positive integer n such that the sum of all the integers 1 + 2 + 3 +... + n is a number between 100 and 1000 whose three digits are identical. As output, the program displays the integer 71 and the corresponding sum.

Solution

%1)
%clearing window, variables and figures
clear all;
close all;
clf;
clc;
ch=1;
while(ch==1)
n=input(\'Enter the number of terms:\');
pro=1;
term=(2)^0.5;
for i=1:1:n
pro=pro*term;
term=(term+(2))^0.5;
end
format long
pro
ch=input(\'\ Do you want to continue, press 1 to continue:\');
end

Result;

Enter the number of terms:5

pro =

20.380016247096115


Do you want to continue, press 1 to continue:1
Enter the number of terms:10

pro =

6.518989025679382e+02


Do you want to continue, press 1 to continue:1
Enter the number of terms:40

pro =

6.999708421902658e+11


Do you want to continue, press 1 to continue:0

%2)
%clearing window, variables and figures
clear all;
close all;
clf;
clc;
x=0;
n=input(\'Enter the number of terms:\');
for i=1:1:n
fprintf(\'\ Enter the %dth term:\',i);
x(i)=input(\'\ Enter:\');
end
for i=1:1:n
for j=1:1:n-i
if x(j)>x(j+1)
a=x(j);
x(j)=x(j+1);
x(j+1)=a;
end
end
end
fprintf(\'\ The sorted matrix:\');
x

Result;

Enter the number of terms:4

Enter the 1th term:
Enter:5

Enter the 2th term:
Enter:2

Enter the 3th term:
Enter:3

Enter the 4th term:
Enter:7

The sorted matrix:
x =

2 3 5 7

%3)
%clearing window, variables and figures
clear all;
close all;
clf;
clc;
sum=0;
n=10;
num=10;
while sum<=100
sum=(n^2/2)+(n/2);
n=n+1;
end
n;
while sum<1000
i=1;
sum=(n^2/2)+(n/2);
if sum>=1000
break
else
num=sum;
while(num>0)
x(i)=rem(num,10);
num=num/10;
num=fix(num);
i=i+1;
end
n1=length(x);
for j=1:1:n1
if j==1
dig=x(j);
elseif x(j)==dig
c=0;
else
c=1;
break;
end
end
if c==0
fprintf(\'\ The sum corresponding to %d is %d:\',n,sum);   
end
n=n+1;
end
end

Result;

The sum corresponding to 36 is 666:

 pi Estimation The value of pi can be estimated from the expression: 2/pi = Squareroot2/2. Squareroot2 + Squareroot2/2. Squareroot2 + Squareroot + Squareroot2/2
 pi Estimation The value of pi can be estimated from the expression: 2/pi = Squareroot2/2. Squareroot2 + Squareroot2/2. Squareroot2 + Squareroot + Squareroot2/2
 pi Estimation The value of pi can be estimated from the expression: 2/pi = Squareroot2/2. Squareroot2 + Squareroot2/2. Squareroot2 + Squareroot + Squareroot2/2

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site