Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
3 views5 pages

New 3

Uploaded by

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

New 3

Uploaded by

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

%% qus3

clear; % Clear workspace variables


clc; % Clear command window
close all; % Close all figures

fprintf('--- Newton-Raphson Solver for Non-Linear System ---\n\n');

% 1. Define Functions and Jacobian


% We use anonymous functions (@) for a clean and simple definition.

% f1(x,y) = (x-1)^2 + (y-2)^2 - 3 = 0


f1 = @(x, y) (x - 1)^2 + (y - 2)^2 - 3;

% f2(x,y) = x^2/4 + y^2/3 - 1 = 0


f2 = @(x, y) x^2 / 4 + y^2 / 3 - 1;

% The vector F contains our two functions


F = @(x, y) [f1(x, y); f2(x, y)];

% The Jacobian Matrix J(x,y)


J = @(x, y) [ 2*(x-1), 2*(y-2);
x/2, 2*y/3 ];

%2. Set Initial Conditions


% An initial guess is required. A good guess can be found by plotting
% the equations. From a sketch, a solution appears near (1.8, 1.0).
X = [1.5; 1.0]; % Initial guess vector [x; y]

tolerance = 1e-4; % Convergence tolerance


max_iterations = 20; % Maximum number of iterations to prevent infinite loops

fprintf('Initial Guess: x = %.4f, y = %.4f\n\n', X(1), X(2));


fprintf('Iter | x | y | Norm(Delta)\n');
fprintf('-----------------------------------------------------\n');

%% 3. Newton-Raphson Iteration Loop


for i = 1:max_iterations
% Get the values of F and J at the current point X
F_val = F(X(1), X(2));
J_val = J(X(1), X(2));

% Solve the linear system J * delta = -F for the update vector 'delta'.
% The backslash operator (\) is MATLAB's efficient way to solve this.
delta = J_val \ -F_val;

% Update our solution vector


X = X + delta;

% Calculate the magnitude (norm) of the change to check for convergence


norm_delta = norm(delta);

% Print the progress of the iteration


fprintf('%d | %11.4f | %11.4f | %e\n', i, X(1), X(2), norm_delta);

% Check if the solution has converged


if norm_delta < tolerance
fprintf('\nConvergence achieved.\n');
break; % Exit the loop
end
end

% 4. Display Final Results


fprintf('\n----------------------------------------\n');
fprintf(' FINAL SOLUTION\n');
fprintf('----------------------------------------\n');
fprintf('x = %.4f\n', X(1));
fprintf('y = %.4f\n\n', X(2));

% Optional: Check if max iterations were reached without converging


if i == max_iterations && norm_delta > tolerance
fprintf('Warning: Maximum iterations reached without converging to the
tolerance.\n');
end

%% ques 2
% -------------------------------------------------------------------------
% MATLAB SCRIPT FOR SUCCESSIVE OVER-RELAXATION (SOR) METHOD
% -------------------------------------------------------------------------

clear;
clc;
close all;

% Define Problem and Parameters


% Ask the user for the value of R
R_val = input('Please enter the value for R (sum of last 3 digits of your roll no):
');

% Define the system matrix A and vector b based on the input R


A = [-R_val, 1, 0, 0;
1, -R_val, 1, 0;
0, 1, -R_val, 1;
0, 0, 1, -R_val];

b = [1; 2; -7; -1];

n = size(A, 1); % Number of equations


tolerance = 1e-6; % Convergence criteria
max_iterations = 100; % Safety limit for iterations

% Define the range for omega


omega_range = 1.25:0.01:1.35;

fprintf('\n======= SOR Method Results =======\n');


fprintf('Solving for R = %d\n', R_val);
fprintf('==================================\n\n');

% Main Loop for Each Omega Value


for omega = omega_range

fprintf('----- Solving for omega = %.2f -----\n', omega);

% Initialize solution vector x for each new omega


x = zeros(n, 1);
% --- SOR Iteration Loop ---
for k = 1:max_iterations
x_old = x; % Store x from the previous iteration

% Loop through each variable/equation


for i = 1:n
% Calculate the sums using vectorization for efficiency
% Sum for terms with new x values (j < i)
sum1 = A(i, 1:i-1) * x(1:i-1);

% Sum for terms with old x values (j > i)


sum2 = A(i, i+1:n) * x_old(i+1:n);

% Apply the SOR formula


x(i) = (1 - omega) * x_old(i) + (omega / A(i,i)) * (b(i) - sum1 -
sum2);
end

% --- Check for Convergence ---


% Stop if the solution is no longer changing significantly
if norm(x - x_old, inf) < tolerance
break;
end
end

% --- Display Results for the current omega ---


if k == max_iterations && norm(x - x_old, inf) >= tolerance
fprintf('Warning: Maximum iterations reached without convergence.\n');
else
fprintf('Convergence was reached in %d iterations.\n', k);
end

fprintf('Solution vector x:\n');


fprintf(' x1 = % .6f\n', x(1));
fprintf(' x2 = % .6f\n', x(2));
fprintf(' x3 = % .6f\n', x(3));
fprintf(' x4 = % .6f\n\n', x(4));
end

%% qus 1

clear; clc;

% Define Truss Properties


Nodes = [ 1,0,0; 2,4,0; 3,8,0; 4,12,0; 5,16,0; 6,20,0; 7,24,0;
8,20,3; 9,16,4; 10,12,4; 11,8,4; 12,4,3 ];

Members = [ 1,2; 2,3; 3,4; 4,5; 5,6; 6,7; 12,11; 11,10; 10,9; 9,8;
1,12; 2,12; 3,12; 3,11; 4,11; 4,10; 4,9; 5,9; 5,8; 6,8; 7,8 ];

Supports = [ 1,1,1; 7,0,1 ]; % [Node, constrain_x, constrain_y]

P = 6;
Loads = [ 3,0,-30; 4,0,-(15+P); 5,0,-30 ]; % [Node, Fx, Fy]

%% 2. Assemble and Solve the System [A]*[F] = [L]


num_nodes = size(Nodes, 1);
num_members = size(Members, 1);
num_reactions = sum(sum(Supports(:, 2:3)));

A = zeros(2 * num_nodes, num_members + num_reactions);


L = zeros(2 * num_nodes, 1);

% Assemble member contributions to matrix A


for i = 1:num_members
node1 = Members(i, 1);
node2 = Members(i, 2);
x1 = Nodes(node1, 2); y1 = Nodes(node1, 3);
x2 = Nodes(node2, 2); y2 = Nodes(node2, 3);
len = norm([x2-x1, y2-y1]); % Using norm for length
c = (x2-x1)/len; % cos_theta
s = (y2-y1)/len; % sin_theta
A(2*node1-1:2*node1, i) = [c; s];
A(2*node2-1:2*node2, i) = [-c; -s];
end

% Assemble support contributions to matrix A


reac_idx = num_members;
for i = 1:size(Supports, 1)
node = Supports(i, 1);
if Supports(i, 2), reac_idx = reac_idx+1; A(2*node-1, reac_idx) = 1; end
if Supports(i, 3), reac_idx = reac_idx+1; A(2*node, reac_idx) = 1; end
end

% Assemble load vector L


for i = 1:size(Loads, 1)
node = Loads(i, 1);
L(2*node-1 : 2*node) = [Loads(i, 2); Loads(i, 3)];
end

% Solve for all forces


Forces = A \ L;

%% 3. Display Results
fprintf('--- Results ---\n');
fprintf('Reaction Forces (kN):\n');
fprintf(' R1x: %7.2f\n R1y: %7.2f\n R7y: %7.2f\n\n', ...
Forces(num_members+1), Forces(num_members+2), Forces(num_members+3));

fprintf('Member Forces (kN) (T=Tension, C=Compression):\n');


for i = 1:num_members
type = ' (T)';
if Forces(i) < 0, type = ' (C)'; end
if Forces(i) == 0, type = ' (Z)'; end
fprintf(' Member %2d (%2d-%2d): %8.2f%s\n', ...
i, Members(i,1), Members(i,2), Forces(i), type);
end

%% q1
% Node Coordinates [x, y] in meters
% Each row represents a node: [node_number, x_coord, y_coord]
Nodes = [
1, 0, 0;
2, 4, 0;
3, 8, 0;
4, 12, 0;
5, 16, 0;
6, 20, 0;
7, 24, 0;
8, 20, 3;
9, 16, 4;
10, 12, 4;
11, 8, 4;
12, 4, 3
];

% Member Connectivity [start_node, end_node]


Members = [
1, 2; % Member 1
2, 3; % Member 2
3, 4; % Member 3
4, 5; % Member 4
5, 6; % Member 5
6, 7; % Member 6
12, 11; % Member 7
11, 10; % Member 8
10, 9; % Member 9
9, 8; % Member 10
1, 12; % Member 11
2, 12; % Member 12
3, 12; % Member 13
3, 11; % Member 14
4, 11; % Member 15
4, 10; % Member 16
4, 9; % Member 17
5, 9; % Member 18
5, 8; % Member 19
6, 8; % Member 20
7, 8 % Member 21
];

% Support Reactions
% [node_number, x_constrained(1/0), y_constrained(1/0)]
% Node 1: Pin (constrains X and Y)
% Node 7: Roller (constrains Y)
Supports = [
1, 1, 1; % [R1x, R1y]
7, 0, 1 % [R7y]
];

% External Loads [node_number, Fx, Fy] in kN


P = 6; % As specified by the user
Loads = [
3, 0, -30;
4, 0, -(15 + P);
5, 0, -30
];

fprintf('Step 1: Truss properties defined.\n');


fprintf(' - %d Nodes\n', size(Nodes, 1));
fprintf(' - %d Members\n', size(Members, 1));
fprintf(' - Load at Node 4 is %.1f kN\n\n', (15 + P));

You might also like