0% found this document useful (0 votes)
10 views4 pages

Numerical 2

The document contains MATLAB code snippets for various mathematical and computational tasks, including damped oscillation plotting, Fibonacci sequence generation, data smoothing, series summation, and approximating the square root using Halley's method. Each section is clearly labeled and includes functions for specific calculations. The code demonstrates the application of numerical methods and data visualization techniques.

Uploaded by

amirabad
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)
10 views4 pages

Numerical 2

The document contains MATLAB code snippets for various mathematical and computational tasks, including damped oscillation plotting, Fibonacci sequence generation, data smoothing, series summation, and approximating the square root using Halley's method. Each section is clearly labeled and includes functions for specific calculations. The code demonstrates the application of numerical methods and data visualization techniques.

Uploaded by

amirabad
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/ 4

Reza Amirabad

% 1 Damped_Oscillation
omega = pi/2;
T = ((2*pi)/omega);
phi = pi/4;
alpha = 0.25;
A=10; % m
t = linspace(0, 3*T, 200);
y = A * exp(-alpha*t).*cos(omega*t + phi);
figure(1);
plot(t,y);
grid on;
title('Damped Oscillation');
ylabel('Position y(t) (m)');
xlabel('Time (s)');

% 2 Fibonacci
clear; clc; close all;
result = fib(1, 1, 10);
fprintf( 'Fibonacci Sequence: \n');
fprintf( '%d ',result);
function fibsequence = fib(x,y,n)
fibsequence = zeros(1, n);
fibsequence(1) = x;
fibsequence(2) = y;
for i = 3:n
fibsequence(i) = fibsequence(i-1) + fibsequence(i-2);
end
end

% 3 Smooth_data
clear; clc; close all;

noisy_data1 = 10.0 + 2.0 * randn(100,1);


smooth_data (noisy_data1);
% noisy data
t = linspace(0, 10, 100);
noise = 1 + 0.25 * randn(100,1);
noisy_data2 = sin(t)' + noise;
smooth_data (noisy_data2);
function smooth_data (x)
numpoints = length (x);
xavg = x;
xavg (1) = x (1);
for n = 2:numpoints
xavg (n) = (x(n) + x(n-1))/2;
end
% normal distributed noisy data
figure;
plot(1:numpoints, x, 'b-', 'DisplayName', 'original data');
hold on;
plot(1:numpoints, xavg, 'r-', 'DisplayName', 'Smoothed Data');
hold off;
xlabel('Time');
ylabel('Data Value');
title('Normal Distributed Noisy Data');
legend;
grid on;
end

% 4 Sum of Series
N = 32;
sum_series = 0;
for k = 0:N-1
sum_series = sum_series + (-1)^k / 2^k;
end
disp('Sum of the series using the for loop:');
disp(sum_series);
k = 0:N-1;
series_elements = (-1).^k ./ 2.^k;
sum_series_elementwise = sum(series_elements);
disp('Sum of the series using elementwise operations:');
disp(sum_series_elementwise);
% 5 Fib_While
clear; clc; close all;
fibw(2, 3, 100);
function Fibonacci_2 = fibw(x,y,n)
Fibonacci_2 = zeros(1, n);
Fibonacci_2(1) = x;
Fibonacci_2(2) = y;
fib_sequence = [x, y];
while true
next_number = fib_sequence(end) + fib_sequence(end-1);
if next_number > n
break;
end
fib_sequence = [fib_sequence, next_number];
end
fprintf( '%d ',fib_sequence);
fprintf( 'Fibonacci Sequence: \n');
end
% 6 Golden_Ratio
clear; clc; close all;
fibw(2, 3, 100, 0.001);
function Fibonacci_2 = fibw(x, y, n, tolerance)
Fibonacci_2 = zeros(1, n);
Fibonacci_2(1) = x;
Fibonacci_2(2) = y;
fib_sequence = [x, y];
prev_ratio = Inf;
while true
next_number = fib_sequence(end) + fib_sequence(end-1);
if next_number > n
break;
end
ratio = fib_sequence(end) / fib_sequence(end-1);
fib_sequence = [fib_sequence, next_number];
if abs(ratio - prev_ratio) < tolerance
break;
end
prev_ratio = ratio;
end
fprintf('Fibonacci Sequence: \n');
fprintf('%d ', fib_sequence);
fprintf('\n');
fprintf('Final ratio (approximating the golden ratio): \n');
fprintf('%f\n', ratio);
end
% 7 mywierdfcn
clc; clear; close all;
x = linspace(-3, 5, 1000);
y = pieces_mywierdfcn(x);
figure;
plot(x, y);
xlabel('x');
ylabel('f(x)');
title(' pieceswise function');
grid on;
function y = pieces_mywierdfcn(x)
y = zeros(size(x));
for i = 1:length(x)
if x(i)>2
y(i) = x(i).^2;
else
y(i) = 2 * x(i);
end
end
end

% 8 Halley_Sqrt
A = 49;
initial_guess = 8;
epsilon = 1e-6;
sqrt_result = my_sqrt(A, initial_guess, epsilon);
fprintf('Approximate square root of %.0d is %.0f \n', A, sqrt_result);
function result = my_sqrt(A, x1, epsilon)
xn = x1;
while true
yn = (1 / A) * xn^2;
xn1 = (xn / 8) * (15 - yn * (10 - 3 * yn));
if abs(xn1 - xn) <= epsilon
break;
end
xn = xn1;
end
result = xn1;
end

You might also like