Sunteți pe pagina 1din 11

to find value of function

function z=f6(X)
x = X(1);
z = zeros(1);
z = x*sin(x) - cos(x/2);

>> feval('f6',[pi/4])
ans =
-0.3685
>>
********************************************************************************

general term for upper triangular backward substitution enteries :-


x(k) = b(k) - a(k,k+1:n) * x(k+1:n) / a(k,k)
********************************************************************************
******
just save...dont save and run
function X=backsub(A,B)
n=length (B);
X= zeros(n,1);
X=B(n)/A(n,n);
for k=n-1:-1:1
X(k)=(b(k)-A(k,k+1:n)*x(k+1:n))/A(k,k);
end
now in command window
>> > A=[3 -2 1 -1;0 4 -1 2;0 0 2 3;0 0 0 5]

>> A
A =
3 -2 1 -1
0 4 -1 2
0 0 2 3
0 0 0 5
>> B=[8;-3;11;15]
B =
8
-3
11
15
>> backsub(A,B)
---------------------------------------------------------
forward subs
function X=forsub(A,B)
n=length (B);
X= zeros(n,1);
X(1)=B(1)/A(1,1);
for k=2:1:1
X(k)=(b(k)-A(k,n:k-1)*x(1:k-1))/A(k,k);
end
********************************************************************************
jacobi iterative method general term
x(j)=(b(j)-a(j,[1:j-1,j+1:n])*P[1:j-1,j+1:n])/a(j,j)

********************************************************************************
*****
now making iterative fucntion in M file

A=[5 -1 1 ; 2 8 -1 ; -1 1 4];
B=[10 ; 11 ; 3];
P=[0 ; 0 ; 0];
n=length(B);
for k = 1:10 .............. 10 is the number of ite
rations
for j=1:n
x(j)=(B(j)-A(j,[1:j-1,j+1:n])*P([1:j-1,j+1:n]))/A(j,j);
end
P=x'
end
x=x' save / debug and run can put the error wala code too if requir
ed as below
********************************************************************************
*****
gauss seidal general term
x(j)=(b(j)-a(j,1:j-1)*x(1:j-1)-a(j,j+1:n)*p(j+1:n))/a(j,j);

gauss sidel functional program

function X=gauss(A,B,P,delta,itr)
n=length(B);
for k=1:itr
for j=1:n
if (j==1)
X(1)=(B(1)-A(1,2:n)*P(2:n))/A(1,1);
elseif (j==n)
X(n)=(B(n)-A(n,1:n-1)*(X(1:n-1))')/A(n,n);
else
X(j)=(B(j)-A(j,1:j-1)*X(1:j-1)-A(j,j+1:n)*P(j+1:n))/A(j,j);
end
end
err=abs(max(X'-P));
P=X';
if (err<delta)
break
end
end
X=X'
only save
now in comand window
> A=[ 1 -5 -1 ; 4 1 -1 ; 2 -1 -6 ]
A =
1 -5 -1
4 1 -1
2 -1 -6
>> B=[ -8 ; 13 ; 2]
B =
-8
13
2
>> P=[0;0;0]
P =
0
0
0
>> delta=0.001;
>> itr=5;
>> gauss(A,B,P,delta,itr)
******************************
plotting sin graph
>> x=[-10:0.1:10];
>> y=tan(x);
>> plot(x,y)
graph saved
>>
in command window
***************************************************************************
to show number of graphs on one graph
>> t=[0:0.05:5];
>> f=exp(-t);
>> g=exp(-2*t);
>> plot(t,f,t,g)
to show g as dotted line
plot(t,f,t,g,'--') or can write the dashes in front of f too
to show colours plot(t,f,'r',t,g,'b')
forn colours and dots or dashes plot(t,f,'r-',t,g,'b*')
graph with grid plot(t,f,t,g, '--'),grid on
graph saved
***************************************************************
Graphs of surface plots..3D
[x y]=meshgrid(-2:0.1:2);
z=y*exp(-x^2 - y^2);
mesh(x,y,z)
graph mesh
mesh(x,y,z)
z=y*exp(-x.^2 - y.^2);
mesh(x,y,z)
graph mesh1
***********************************************
to show surface
surf(x,y,z) instead of mesh(x,y,z)
graph surf
*************************************************************************
finding function value
function z=f2(X)
x=X(1);
z=zeros(1);
z=(x^3)-3*x^2-x+3;
in command window
feval('f2',[0])
ans =
3
**************************************************************************
bisection method iterations
first save function by name of m3
function z=f2(X)
x=X(1);
z=zeros(1);
z=(x^3)-3*x^2-x+3;
close m file
new m file
n=100;
tolerance=0.001;
a=0;
c=3;
ya=feval('m3',[a]);
yc=feval('m3',[c]);
if (ya*yc)>0
disp('ya*yc is positive')
break
end
for i=1:n
b=(a+c)/2;
yb=feval('m3',[b]);
if (abs(b-a))<tolerance
disp('tolerance is satisfied')
disp('final result approximately is equal to')
disp(b)
break
end
if (ya*yb)<0
c=b
yc=yb;
else
a=b;
ya=yb;
end
end
save and run as untitled

in command window

c =
1.5000

c =
1.1250

c =
1.0313

c =
1.0078

c =
1.0020
c =
1.0005
tolerance is satisfied
final result approximately is equal to
0.9998
********************************************************************************
*******************************
Newton raphson method
function z=fn(X)
x=X(1);
z=zeros;
z=x^3-3*x-2
just save
___________________
function z=fnt(X)
x=X(1);
z=zeros;
z=3*x^2-3;
_____________
x(1)=0.2;
n=100;
for i=1:n
yx=feval('fn',[x(i)]);
yxt=feval('fnt',[x(i)]);
x(i+1)=x(i)-(yx/yxt);
if abs(x(i+1)-x(i))<0.0001
disp(': Iteration Stop :')
disp(x(i+1))
break
end
end
save and run
********************************************************************************
*************************************
Curve fitting
>> x = [ 0.1 0.4 0.5 0.7 0.8 0.9];
x =
0.1000 0.4000 0.5000 0.7000 0.8000 0.9000

>> y = [ 0.61 0.92 0.99 1.52 1.47 2.03 ];


y =
0.6100 0.9200 0.9900 1.5200 1.4700 2.0300
___________________________
SUM OF VALUES OF X AND Y

>> sumx = sum(x);


sumx =
3.4000
>> sumy = sum(y);
sumy =
7.5400
_________________________________

SUM OF SQUARES OF VALUES OF X AND Y

>> sumx2 = sum(x.^2);


sumx2 =
2.3600
>> sumy2 = sum (y.^2);
sumy2 =
10.7908
_________________________________________________

SUMMATION OF PRODUCT OF CORESPONDING X AND Y VALUES

>> sumxy = sum (x.*y);


sumxy =
4.9910
____________________________________________________________
>> n = length (x)
n =
6
__________________________________________________________
NEXT THREE STEPS BECAUSE R = A / (B*C)
____________________________________________________________
>> a = ((n * sumxy) - (sumx * sumy))

a =
4.3100

_____________________________________________________________________
>> b = sqrt (n * sumx2 - sumx*sumx )

b =
1.6125
_____________________________________________________________________
>> c = sqrt (n * sumy2 - sumy*sumy )
c =
2.8095
___________________________________________________________________________
>> r = a/(b*c)
r =
0.9514

________________________________________________________________________________
_________________________

ALTERNATIVELY IF WE PUT SEMICOLANS AT THE END RESULT WILL NOT BE DISPLAYED AND V
ALUES WILL BE DIRECTLY STORED

>> x = [ 0.1 0.4 0.5 0.7 0.8 0.9];


>> y = [ 0.61 0.92 0.99 1.52 1.47 2.03 ];
>> sumx = sum(x);
>> sumy = sum(y);
>> sumx2 = sum(x.^2);
>> sumy2 = sum (y.^2);
>> sumxy = sum (x.*y);
>> n = length (x);
>> a = ((n * sumxy) - (sumx * sumy));
>> b = sqrt (n * sumx2 - sumx*sumx );
>> c = sqrt (n * sumy2 - sumy*sumy );
>> r = a/(b*c);
>> r
______________________________________________________________________________

>> A = [ 6 3.4 ; 3.4 2.36 ]


A =
6.0000 3.4000
3.4000 2.3600
>> B = [ 7.54 ; 4.991]
B =
7.5400
4.9910
>> X = inv(A)* B
X =
0.3173....................................x1
1.6577....................................x2
p = 0:0.1:1;
>> g= 0.3173 + 1.6577*p;
________________________________________________________________________________
__________________

>> plot (p,g)


>> hold on
>> plot (x,y)
***************************************************************
Curve fitting sin theta wali example
QFit a curve Y=A+Bx+C Sin(Pi*X)+D Sin(2*pi*X)

X=[0.1 0.2 0.3 0.4 0.5 0.6];


y=[0 2.1220 3.0244 3.2568 3.1399 2.8579];

sumX=sum(X);
sumy=sum(y);
sumX2=sum(X.^2);
sumsinpiX=sum(sin(pi.*X));
sumsin2piX=sum(sin(2*pi.*X);
sumXisinpiX=sum(X.*sin(pi.*X));
sumXisin2piX=sum(X.*sin(2*pi.*X));
sunXy=sum(X.*y);
sumyi.sinpiX=sum(y.*sin(pi.*X);
sumyi.sinpiX=sum(y.*sin(pi.*X));
sumsinpix2=sum(sin(pi.*X).*sin(pi.*X));
sumyisin2piX=sum(y.*sin(2*pi.*X));
sumsinpix.sin2pix=sum(sin(pi.*X)*sin(2*pi.*X));
A=[6 2.1000 4.6079 2.4899;2.1000 0.9100 1.8422 0.4167;4.6079 1.8422 2.5294 1.510
1;2.4899 0.4167 1.5101 3.9045]

A =
6.0000 2.1000 4.6079 2.4899
2.1000 0.9100 1.8422 0.4167
4.6079 1.8422 2.5294 1.5101
2.4899 0.4167 1.5101 3.9045

>> B=[14.4010;5.9191;12.6494;5.1290]

B =
14.4010
5.9191
12.6494
5.1290
>> X=inv(A)*B
X =
-0.4514
7.5601
-0.2045
0.8737

>> plot(x,y)
>> p=0.1:0.1:0.6;
>> g=-0.4514+7.5601*p-0.2045*sin(pi*p)+0.8737*sin(2*pi*p);
>> plot(p,g)
>> hold on
>> plot(x,y)
********************************************************************************
********
LANGRANGE FORMULA
_________________________________________________
basics to write equation
>> poly(2)
ans =
1 -2
>> poly(1)
ans =
1 -1
>> p=poly(1);
>> q=poly(2);
>> conv(p,q)
ans =
1 -3 2
>>
>>
________________________________________________
write its function in M file now

function [ C L ] = lagran (x ,y)


w = length (x);
n= w-1;

for N = 1:n+1
v =1;
for k=1:n+1
if N ~= k
v = conv ( v , poly (x(k)))/ ( x(N) - x ( k));
end
end
L (N, :) = v ;
end
C = y * L;

****************************************************************
NOW MAIN WINDOW
>> x = [1 2 3 ];
>> y = [ 3 4 5 ];
>> lagran (x,y)

S-ar putea să vă placă și