0% found this document useful (0 votes)
84 views9 pages

Examples of Matlab 24-7-18

The document contains code for several different purposes: 1) Approximating the sine function using a Taylor series expansion up to a specified number of terms. 2) Checking values in a matrix and printing whether they are above or below a threshold. 3) Calculating Fibonacci numbers up to a specified index. 4) Filling a matrix using nested for loops.

Uploaded by

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

Examples of Matlab 24-7-18

The document contains code for several different purposes: 1) Approximating the sine function using a Taylor series expansion up to a specified number of terms. 2) Checking values in a matrix and printing whether they are above or below a threshold. 3) Calculating Fibonacci numbers up to a specified index. 4) Filling a matrix using nested for loops.

Uploaded by

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

Code for sin series

function [out] = ApproxSin (x,n);


out=[];
out(1) = 0;
sm=0;
for k = 0:n-1;
out (k+1) = (-1)^k * x^(2*k+1)/factorial(2*k+1);
sm=sm+ out (k+1);
end
disp([out])
disp(sm)
y = sin(x)

percenterror = (abs((sm - y)/y))*100

% function out = ApproxSin (x,n)


% out = 0;
%
% for k = 0:n-1;
% out = out + (-1)^k * x^(2*k+1)/factorial(2*k+1);
% end
%
% y = sin(x)
%
% percenterror = (abs((out - y)/y))*100
%
Code for value check in matrix
close all
clear all
matA=[1.4,4.2,6.7,7.0;5.5,6.7,8.9,3.0;0.6,6.12,5.44,8.94]
[row,col] = size(matA)

for i = 1:1:row
for j = 1:1:col
currNo = matA(i,j);
st1 = strcat('The value being tested is: ', num2str(currNo),'.');
disp(st1)

if (currNo > 3)
disp('The current value is larger than 3.')
else
disp('The current value is less or equal than 3.')
end
end
end
Don’t know what the shit this does
x = 1:10;
y = x*t^2; % just an example
t = 1:10;
m = [x' zeros(numel(x),numel(t))]; % preallocate and fill in first column
with 'x'
for r = 1:numel(x) % row counter
for c = 1:numel(t) % column counter
m(r,c+1) = y(x(r),t(c)); % note the 'c+1' column subscript for the
output matrix
end
end
Fibonacci
n=10;
f = zeros(n,1);
f(1) = 0;
f(2) = 1;
for k = 3:n
f(k) = f(k-1) + f(k-2);
end
disp(f')
Matrix loop
% 1 2 3 4 5
% 2 4 6 8 10
% 3 6 9 12 15

A=zeros(3,5);

A(1,1)=1; A(2,1)=2; A(3,1)=3;


%For loops can be nested as desired:
for n=1:3
for m=1:4

if n==1
A(n,m+1)=A(n,m)+1;

elseif n==2
A(n,m+1)=A(n,m)+2;
else
A(n,m+1)=A(n,m)+3;
end
end
end
disp(A)
For loop
%For loops can be nested as desired:
for n=1:3
for m=3:-1:1
A(n,m)=n^2+m^2;
end
end
disp(A)
Plot

function [v]=plotit(xmin,xmax,npoints);
% plot a graph of a function of a single variable f(x)
% from xmin to xmax, with npoints data points

for n=1:npoints
x(n) = xmin + (xmax-xmin)*(n-1)/(npoints-1);
v(n)=cos(x(n));
end
figure;
plot(x,v);
end
Prime
clc
x=input('give any natural number')
if x==1;
p={'not prime'};
elseif x==2;
p={'prime'};
else
for i=3:x
if rem (x,(i-1))==0 ;
p={'not prime'};
break
else
p={'prime'};
end
end
end
p
Series sum
n=input ('Enter upto how many terms you want to see & sum: ');
num=0;sum=0;num1=[];
for x=1:n
num = num+5;

num1 (x) = num;


sum=sum+num;
end
disp('the series is: ');
disp(num1);
disp('the sum is: ');
disp(sum);

You might also like