EEE 306

EEE 306 Lab 8 : Introduction to Matlab

This Lab is about an introduction to matlab.

The objective of this lab is to learn about

  • Basic mathematical and logical operator in matlab
  • Working with Matrix and vector
  • Use of conditional ‘if’
  • Use of for and while loop

Practice script

 %Basic matrix operation
 a=[1 2 3;4 5 6;7 8 9]
 b=[3 2 1;6 5 4;9 8 7]
 c=a+b
 d=a*b
 n=5
 e=a+n*c

%Use of if, while and for
%while 1 %Try not to use while 1 in matlab. It is not a good idea.
total=0
for i=0:1:5
mark= input('What is the mark ')

if mark >=90 
 point=5;
elseif mark>=80 
 point=4;
elseif mark>=70 
 point=3;
elseif mark>=60 
 point=2;
elseif mark>=50 
 point=1;
else
 point=0;
end % end of if
total=total+point;
end %end of for loop

total=total/6

%end % end for while loop

MATLAB intro

EEE 306 Lab 7 : Open methods for finding roots

Task #1

Write a program to use fixed point iteration to solve the equation f(x)=e^(-x) – x where inputs will be the initial guess, maximum number of iterations and maximum error (es). If the approximation error (ea) is less than es your program should stop continuing any iterations.

Task #2

Write a program to use Newton Raphson Method to solve the equation f(x)=e^(-x) – x where inputs will be the initial guess, maximum number of iterations and maximum error (es). If the approximation error (ea) is less than es your program should stop continuing any iterations.

Task #2b

Modify your code in 2a to solve for a quadratic equation.

Task #3

Write a program to use  Secant Method to solve the equation f(x)=e^(-x) – x where inputs will be the initial guess, maximum number of iterations and maximum error (es). If the approximation error (ea) is less than es your program should stop continuing any iterations.

EEE 306 Lab 6 : Bracketing methods for finding roots

Task #1

Write a program to find root of an equation in the form ax^2+bx+c, where a,b,c, will be taken as input. You have to take input the number of iteration and the termination criterion.

Use bisection method to find the root where the upper limit and lower limit will be taken as input.

 

Task #2

Write a program to find root of an equation in the form ax^2+bx+c, where a,b,c, will be taken as input. You have to take input the number of iteration and the termination criterion.

Use False Position method to find the root where the upper limit and lower limit will be taken as input.

 

EEE 306 Lab 5 : Taylor series sine and cosine

Task #1

Write a program to find an estimate of any value of cos(x). You should take input the value of x in degree, the maximum number of iteration, and the minimum error requirement

Sample code

#include<stdio.h>
#include<math.h>
int fact(int n)
{
 if (n==1){return 1;}
 return n*fact(n-1);
}
int main()
{
 int i,j, iter, sign=1;
 double x, es,ea, previous=1, approx=1, curr_approx;
 scanf("%lf", &x);
 x=(x/180)*3.141592654;
 //printf("%lf\n", x);
 scanf("%d", &iter);
 scanf("%lf", &es);
 for (i=1;i<iter;i++)
 {
 j=i*2;
 curr_approx=(pow(x,j)/(fact(j)));
 if (sign%2==1)
 {
 approx=approx-curr_approx;
 }
 else {approx=approx+curr_approx;}

ea=fabs(((approx-previous)/approx)*100);
 previous=approx;
 sign++;
 printf("\n %lf\t %lf", approx, ea);
 if (ea<es) break;

}

printf("\n%lf", approx);
}

Task #2

Write a program to find an estimate of any value of sin(x). You should take input the value of x in degree, the maximum number of iteration, and the minimum error requirement

Sample code

#include<stdio.h>
#include<math.h>
int fact(int n)
{
 if (n==1){return 1;}
 return n*fact(n-1);
}
int main()
{
 int i,j, iter, sign=0;
 double x, es,ea, previous=0, approx=0, curr_approx;
 scanf("%lf", &x);
 x=(x/180)*3.141592654;
 //printf("%lf\n", x);
 scanf("%d", &iter);
 scanf("%lf", &es);
 for (i=1;i<iter;i++)
 {
 j=(i*2)-1;
 curr_approx=(pow(x,j)/(fact(j)));
 if (sign%2==1)
 {
 approx=approx-curr_approx;
 }
 else {approx=approx+curr_approx;}

ea=fabs(((approx-previous)/approx)*100);
 previous=approx;
 sign++;
 printf("\n %lf\t %lf", approx, ea);
 if (ea<es) break;

}

printf("\n%lf", approx);
}

Assignment

There is no assignment for this lab.

EEE 306 Lab 3 : Sand castle in C beach

P.S. This lab is not related to numerical analysis. This lab is about basic C and is on students request.

Problem #1

Print a half pyramid

#include <stdio.h>

int main() {
 int i, j, n;
 printf("\nNumber of line is ");
 scanf("%d",&n);

for(i = 0; i < n; i++) {
 for (j = 0; j <= i; j++) {
 printf("*");
 }
 printf("\n");
 }
 getchar();
 return 0;
}

 Problem #2

Draw a 5 level pyramid

#include <stdio.h>

int main() {
 char line1[] = "*";
 char line2[] = "**";
 char line3[] = "***";
 char line4[] = "****";
 char line5[] = "*****";

printf("%40s\n",line1);
 printf("%40s*\n",line2);
 printf("%40s**\n",line3);
 printf("%40s***\n",line4);
 printf("%40s****\n",line5);

return 0;
}

 Problem #3

Scan for n inputs and only take positive inputs

#include <stdio.h>

int main() {
 int arr[100], n, i, temp;

scanf("%d", &n);

i = 0;
 while(i < n) {
 scanf("%d",&temp);

if(temp > 0) {
 arr[i] = temp;
 i++;
 }
 }

for(i = 0; i < n; i++) {
 printf("%d ", arr[i]);
 }

return 0;
}

 Problem #4

Take a string input and check for number of vowels in the string

#include<stdio.h>
int main()
{
 int i, vowel=0;
 char str[1300];
 printf("type ur string\n\n");
 scanf("%[^\n]s", &str);
 for(i=0; str[i]!='\0'; i++){
 if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u'||str[i]=='A'||str[i]=='E'||str[i]=='I'||str[i]=='O'||str[i]=='U')
 {
 vowel++;
 }}
 printf("no of vowel is %d", vowel);
}

 Problem #5

Take a string input and show the number of letters in the string and convert them to uppercase

#include <stdio.h>
#include <ctype.h>

int main() {
 char str[1000];
 int i;

scanf("%[^\n]s", str);

for(i = 0; str[i]; i++) {
 str[i] = toupper(str[i]);
 }

printf("Length = %d\n", i);
 printf("%s", str);

return 0;
}

 Problem #6

Take a number input and check if the number is prime or not.

#include <stdio.h>
int main()
{
int i, n, j=0;
printf("The number to be tested is ");
scanf("%d", &n);

for (i=1; i <= n; i++)

{ if ( n % i == 0 ) {j++;} 
 }

if(j==2)
{printf("%d is a prime number.", n);
}
else {printf("%d is not a prime number", n);
}
return 0;
}

EEE 306 Lab 2 : Taylor’s error

Problem#1
Find the sum of the n terms for the sequence
1/(x^n)
Input: x n
output:
Term_no sum
for all term leading to n
Example input:
Example Output:

 

Problem#2
Find the approximation of e^x using maclauren series for %Ea <n%
Input: x n
Output:
interation_no approximation %Ea
for every iteration
Example input:

0.5 0.0001

Example Output:

2 Approximation=1.500000 Error=50.000000
3 Approximation=1.625000 Error=8.333333
4 Approximation=1.645833 Error=1.282051
5 Approximation=1.648438 Error=0.158228
6 Approximation=1.648698 Error=0.015798
7 Approximation=1.648720 Error=0.001316
8 Approximation=1.648721 Error=0.000094

Approximation for e^0.500000 with 0.000094% error is 1.648721

 

Submit Assignment 2 through this link