Thursday, May 22, 2014

Matlab Codes for Numerical Methods.

Hey guys! So I mentioned earlier that I will share the algorithms I programmed. The night before the deadline of checking, some of my friends came over to my house. But before that, we were working on a Fixed Point Iteration code in the afternoon when manong (term used to refer to a male that is older than you, manang for a female.) Karlo asked me to try a code for HIM (get it. lol) In there, it says the we must use an "inline" function. We didn't know both what it was so I searched it over the internets. Turns out it is one of the key in programming these solutions. The other one is the eval() function. I learned that while doing a False Position code.

(I apologize for the code. I couldn't use the blockquote tag because I can't get it to look right.)


NOTE: Please clear the data in the Workspace window before proceeding to solve another problem. Maybe that was the reason why I couldn't get the 'stop iteration when error=certain value'  because the values are the same.


Anyway, this is the code for Fixed Point Iteration.

----------------------------------------------------------------------------------------

%Fixed Point Iteration V1.0
%Feel free to edit these codes
%This code will not check for convergence :(
%No option to stop iteration when error=certain value
%Codes by Shane Pucan
%For ECE 174 (Numerical Methods)
%iknshane.blogspot.com

f=input('Enter the function: ','s'); %The input must be in String
x=input('Enter the initial Value: ');
k=input('Enter number of Iterations: ');

fx=inline(f); %Our function f is put into an inline function by fx
fprintf(' k Xk X(k+1) \n') %This is the header for our table, adjust so that it will line up the the data
for i=1:k
xk=fx(x); %With the inline function, any value of x will be substituted and evaluated into the function f
fprintf('%0.4f %8.4f %9.4f \n',i,x,xk) %This will display the data through the iterations
x=xk;
end

Fixed Point Iteration Using Matlab Codes Example

----------------------------------------------------------------------------------------

Here's Newton's Method. 

%Newton's Method V1.0
%Feel free to edit these codes
%No option to stop iteration when error=certain value
%Codes by Shane Pucan
%For ECE 174 (Numerical Methods)
%iknshane.blogspot.com

f=input('Enter the function: ','s');
xo=input('Enter initial Value: ');
k=input('Enter number of iterations: ');

syms x %v
y=diff(f); %this code will differentiate f with respect to x

fn=inline(f);
fprintf(' k Xk X(k+1) \n') %This is the header for our table, adjust so that it will line up the the data
for i=1:k
x=xo; %This is necessary so that our y (f'(x)) can be evaluated by our initial value
fy=eval(y); %This will evaluate y using the values of our initial value
xk=(xo)-((fn(xo))/(fy)); %Newton's Iterative Formula
fprintf('%f %8.4f %9.4f \n',i,xo,xk) %This will display the data through the iterations
xo=xk;
end
Newton's Method Using Matlab Codes Example
----------------------------------------------------------------------------------------

Gauss-Siedel Method

%Gauss-Siedel Method V1.0
%Feel free to edit these codes
%Limited only to 4x4 Matrix (Can be tweaked to solve n bymatrix)
%Codes by Shane Pucan
%For ECE 174 (Numerical Methods)
%iknshane.blogspot.com
disp('This program is only limited to 4x4 Matrices')
disp('')
A=input('Please enter the matrix A: ');
b=input('Please enter the constants b: ');
n=size(A);
xo(1)=input('Please enter the initial value of Xo1: '); %our xo is treated as a vector
xo(2)=input('Please enter the initial value of Xo2: ');
xo(3)=input('Please enter the initial value of Xo3: ');
xo(4)=input('Please enter the initial value of Xo4: ');
I=input('Please enter the desired number of Iterations: ');

for k=1:I
xo(1)=(b(1)-A(1,2)*xo(2)-A(1,3)*xo(3)-A(1,4)*xo(4))/A(1,1);
xo(2)=(b(2)-A(2,1)*xo(1)-A(2,3)*xo(3)-A(2,4)*xo(4))/A(2,2);
xo(3)=(b(3)-A(3,1)*xo(1)-A(3,2)*xo(2)-A(3,4)*xo(4))/A(3,3);
xo(4)=(b(4)-A(4,1)*xo(1)-A(4,2)*xo(2)-A(4,3)*xo(3))/A(4,4);
end

disp('')
disp('    X1         X2           X3            X4')
disp('------------------------------------------------')
disp(xo)

----------------------------------------------------------------------------------------
Jacobi's method is a lot like Gauss-Siedel. The only difference is it uses the same value of xk for each iteration, thus making it slower to arrive at the solution.
...
Hahaha. Crap. I think I lost the code for jacobi. I think I overwrote it with the gauss-siedel. Sorry. Well, that's it for now. I'll ask if my friend copied the original code. Till then. Enjoy coding and enjoy your summer! Cheers! ✌

No comments:

Post a Comment