Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
Final Release May 17 1042PM
  • Loading branch information
mohamed-shaapan committed May 17, 2017
1 parent a280aa7 commit 6985406
Show file tree
Hide file tree
Showing 29 changed files with 797 additions and 110 deletions.
1 change: 1 addition & 0 deletions src/input_files/birge_vieta.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x^3-3 -1 5 50 0.0001
5 changes: 5 additions & 0 deletions src/input_files/linear_systems/siedel.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
3
x y z
12*x+3*y-5*z=1
x+5*y+3*z=28
3*x+7*y+13*z=76
5 changes: 5 additions & 0 deletions src/input_files/linear_systems/tests.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
3
x y z
2*x+y+4*z=1
x+2*y+3*z=1.5
4*x-y+2*z=2
5 changes: 5 additions & 0 deletions src/input_files/linear_systems/tests2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
3
x1 x2 x3
2*x1+x2+4*x3=1
x1+2*x2+3*x3=1.5
4*x1-x2+2*x3=2
6 changes: 6 additions & 0 deletions src/input_files/linear_systems/tests3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
4
x1 x2 x3 x4
2*x1+x2+4*x3+5*x4=1
x1+2*x2+3*x3-x4=1.5
4*x1-x2+2*x3+2*x4=2
3*x1+3*x2-2*x3+3*x4=6
1 change: 1 addition & 0 deletions src/input_files/test1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x^3-3 -1 5 50 0.0001
Binary file modified src/linear_systems/MatlabAssignment.fig
Binary file not shown.
61 changes: 47 additions & 14 deletions src/linear_systems/MatlabAssignment.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
function varargout = MatlabAssignment(varargin)
addpath('./logic');
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
Expand Down Expand Up @@ -67,6 +66,7 @@ function solveBtn_Callback(hObject, eventdata, handles)
eqn={};
labels=get(handles.edit9, 'string');
eval(['syms ' labels]);
num=numel(data);
for i=1:numel(data)
disp(data(i));
finalres=[finalres;char(data(i))];
Expand Down Expand Up @@ -114,13 +114,32 @@ function solveBtn_Callback(hObject, eventdata, handles)
elseif (popChoice==4)
init=str2double(strsplit(get(handles.edit12,'string'),' '));
maxIter=str2double(get(handles.edit11,'string'));
if isnan(maxIter)
maxIter= 50;
end
epsilon=str2double(get(handles.edit10,'string'));
w=gauss_siedel(A,B,init,maxIter,epsilon);
w = strread(num2str(w),'%s');
d=cat(2,d,w);
set(handles.uitable6,'Data',d);
set(handles.uitable6,'Visible','on')

if isnan(epsilon)
epsilon= 0.0001;
end

[w, iterations, data] =gauss_siedel(A,B,init,maxIter,epsilon);
w = strread(num2str(w),'%s');
d=cat(2,d,w);
set(handles.uitable5,'Data',data);
set(handles.uitable6,'Data',d);
set(handles.uitable6,'Visible','on');
set(handles.uitable5,'Visible','on');
x2=1:iterations;
hold off
for i=1:num
y2=data(:,i);
disp('************************');
disp(y2);

plot(x2,y2,'m');
hold on
scatter(x2,y2,'k','x');
end
end

%set(handles.show,'String',char(m));
Expand Down Expand Up @@ -163,29 +182,37 @@ function loadBtn_Callback(hObject, eventdata, handles)
fullPath = strcat(pathname,filename);
fileID = fopen(fullPath);
tline = fgetl(fileID);
num=str2double(tline)
labels = fgetl(fileID)
num=str2double(tline);
labels = fgetl(fileID);
tline = fgetl(fileID);
finalres={};
eqn={};
while ischar(tline)
for i=1:num
disp(tline);
finalres=[finalres;tline];
tline = fgetl(fileID);

end

disp(finalres);
set(handles.tableContent,'Data',finalres)
eval(['syms ' labels]);


fclose(fileID);

for c = 1:num
eqn = [eqn,eval(strrep(char(finalres(c)),'=','=='))]
eqn = [eqn,eval(strrep(char(finalres(c)),'=','=='))];
end
[A,B] = equationsToMatrix(eqn)

ss=sprintf('%s\n%s',char(B),char(A));
set(handles.show,'String',ss);
%[A,B] = equationsToMatrix(eqn);
set(handles.edit1,'String',num);
set(handles.edit9,'String',labels);
Ncolumns=1;
Nrows=num;

set(handles.tableContent,'Visible','on')
set(handles.tableContent,'columnname','Equations')



Expand Down Expand Up @@ -324,3 +351,9 @@ function edit12_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end


% --- Executes during object creation, after setting all properties.
function axes1_CreateFcn(hObject, eventdata, handles)
pan on
% Hint: place code in OpeningFcn to populate axes1
19 changes: 19 additions & 0 deletions src/linear_systems/console_tests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
epsilon = 0.0001;
max_iterations = 50;

%test case 1
%coeff_matrix=[2,1,4; 1,2,3; 4,-1,2];
%constants_matrix=[1,1.5,2];

%test case 2
coeff_matrix=[12,3,-5; 1,5,3; 3,7,13];
constants_matrix=[1,28,76];


initial_guess=[1,0,1];


addpath('./logic');
[solution, iterations, data] = gauss_siedel(coeff_matrix, constants_matrix, initial_guess, max_iterations, epsilon);

disp(solution);
67 changes: 46 additions & 21 deletions src/linear_systems/logic/gauss_siedel.m
Original file line number Diff line number Diff line change
@@ -1,21 +1,37 @@
function [solution, iterations, data] = gauss_siedel(coeff_matrix, constants_matrix, initial_guess, max_iterations, epsilon)
addpath('./utilities')

function [solution, iterations, data] = gauss_siedel(coeff_matrix, constants_matrix, initial_guess, max_iterations, epsilon)
addpath('./utilities');

%disp(coeff_matrix);
%disp(constants_matrix);


tic
system_matrix = create_system_matrix(coeff_matrix, constants_matrix);

disp(system_matrix);

num_of_unknowns = length(constants_matrix);
[solution, iterations, data] = implementation(system_matrix, initial_guess, num_of_unknowns, max_iterations, epsilon, 0, []);
timeElapsed = toc;
%timeElapsed = toc;
%disp(data);
disp('ITERATIONS');
disp(iterations);




% output the results in file in table format.
output_file = strcat('./logic/outputs/gauss_siedel_', datestr(clock),'.txt');
%output_file = strcat('./outputs/gauss_siedel_', datestr(clock),'.txt');
date_ = strrep(datestr(clock),':','_');
output_file = strcat('./linear_systems/outputs/gauss_siedel_', date_,'.txt');


fileID = fopen(output_file, 'w');


% fill the colheadings
%fill the colheadings
colheadings = {};
% fill the x's x1, x2, x3 ..
%fill the x's x1, x2, x3 ..
for i=1:num_of_unknowns,
colheadings{end+1} = strcat('x',int2str(i));
end
Expand All @@ -24,7 +40,7 @@
colheadings{end+1} = strcat('Err_x',int2str(i));
end

% fill the rowheadings
%fill the rowheadings
rowheadings = {};
for i=1:iterations,
rowheadings{end+1} = int2str(i);
Expand All @@ -50,33 +66,35 @@
function [solution, iterations, data] = implementation(system_matrix, previous_solutions, num_of_unknowns, max_iterations, epsilon, iterations, data)

current_solutions=previous_solutions;


%disp(previous_solutions);

tmp_data=[];
approximations = [];
errors = [0, 0, 0];
for row=1:num_of_unknowns

% disp(system_matrix);


a=system_matrix(row, row);
d=system_matrix(row, num_of_unknowns+1);
%disp(d);

summation=0;
index=1;
%index=1;
for col=1:num_of_unknowns
%disp(index);
if(row==col)
index = index+1;
%index = index+1;
continue;
end
%disp(col);
% disp(system_matrix(row, col));
%disp(system_matrix(row, col));

summation = summation + system_matrix(row, col)*previous_solutions(index);
index = index+1;
summation = summation + system_matrix(row, col)*previous_solutions(col);
%index = index+1;
end

answer = (1/a)*(d-(summation));
answer = (1/a)*(d-summation);
%disp(answer);
current_solutions(row)=answer;

Expand All @@ -95,6 +113,8 @@
stop_algorithm = stop_condition_test(previous_solutions, current_solutions, max_iterations, iterations, epsilon);
if(stop_algorithm==true)
solution = current_solutions;
%iterations

return;
end

Expand All @@ -121,10 +141,9 @@
end

end




test=false;

end


Expand All @@ -137,4 +156,10 @@

system_matrix=result;

end
end






19 changes: 19 additions & 0 deletions src/linear_systems/outputs/gauss_siedel_17-May-2017 21_40_49.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
| x1| x2| x3| Err_x1| Err_x2| Err_x3|
1| -3.2500| 21.6000| 21.2308| 5.1538| 1.9259| 1.9420|
2| 3.5295| -6.4885| -5.0346| 1.9208| 4.3290| 5.2170|
3| -0.3923| 7.9149| 8.5254| 9.9967| 1.8198| 1.5905|
4| 1.6569| 0.5632| 1.6748| 1.2368| 13.0535| 4.0903|
5| 0.6404| 4.2637| 5.1605| 1.5873| 0.8679| 0.6755|
6| 1.1676| 2.3756| 3.4025| 0.4516| 0.7948| 0.5167|
7| 0.9072| 3.3250| 4.2975| 0.2871| 0.2855| 0.2083|
8| 1.0427| 2.8401| 3.8464| 0.1300| 0.1707| 0.1173|
9| 0.9760| 3.0836| 4.0763| 0.0684| 0.0790| 0.0564|
10| 1.0109| 2.9590| 3.9605| 0.0345| 0.0421| 0.0292|
11| 0.9938| 3.0215| 4.0195| 0.0172| 0.0207| 0.0147|
12| 1.0028| 2.9895| 3.9899| 0.0089| 0.0107| 0.0074|
13| 0.9984| 3.0055| 4.0050| 0.0044| 0.0053| 0.0038|
14| 1.0007| 2.9973| 3.9974| 0.0023| 0.0027| 0.0019|
15| 0.9996| 3.0014| 4.0013| 0.0011| 0.0014| 0.0010|

number of iterations: 15
execution time: 0.772864
Binary file modified src/root_finding/MatlabAssignment.fig
Binary file not shown.
Loading

0 comments on commit 6985406

Please sign in to comment.