matlab assignment
matlab assignment
Problem 1
x = linspace(-3,5,500);
f = (x+5).^2 ./ (4+3*x.^2);
figure;
plot(x, f, 'b-', 'LineWidth', 2);
xlabel('x'); ylabel('f(x)');
title('Problem 1: (x+5)^2 / (4+3x^2)');
grid on;
Problem 2
x = linspace(-3,5,500); % Same x range for fairness
f = sin(5*x) ./ (x + exp(-0.75*x));
plot(x, f, 'r--', 'LineWidth', 2);
xlabel('x');
ylabel('f(x)');
title('Plot of f(x) = sin(5x) / (x + e^{-0.75x})');
grid on;
Problem 3
% First plot
subplot(2,1,1); % 2 rows, 1 column, plot 1
x1 = linspace(-3,0,300);
f1 = (x1+5).^2 ./ (4 + 3*x1.^2);
plot(x1, f1, 'm-', 'LineWidth', 2);
title('Plot for -3 <= x <= 0');
xlabel('x');
ylabel('f(x)');
grid on;
% Second plot
subplot(2,1,2); % 2 rows, 1 column, plot 2
x2 = linspace(0,5,300);
f2 = (x2+5).^2 ./ (4 + 3*x2.^2);
plot(x2, f2, 'g-', 'LineWidth', 2);
title('Plot for 0 <= x <= 5');
xlabel('x');
ylabel('f(x)');
grid on;
Problem 4
fplot(@(x) (x+5).^2 ./ (4+3*x.^2), [-3 5], 'LineWidth', 2);
xlabel('x'); ylabel('f(x)');
title('Problem 4: Using fplot');
grid on;
Problem 5
x = linspace(0,2*pi,500);
y1 = sin(x); y2 = cos(x);
plot(x, y1, 'b-', 'LineWidth', 2);
hold on;
plot(x, y2, 'r--', 'LineWidth', 2);
hold off;
legend('sin(x)', 'cos(x)');
xlabel('x'); ylabel('y');
title('Problem 5: sin(x) and cos(x)');
grid on;
Problem 6
figure;
plot(x, y1, 'k-', 'LineWidth', 2);
hold on;
line(x, y2, 'Color', 'm', 'LineStyle', '--', 'LineWidth', 2);
hold off;
legend('sin(x)', 'cos(x)');
xlabel('x'); ylabel('y');
title('Problem 6: using line command');
grid on;
Problem 7
x = linspace(0,2*pi,100);
y = sin(x);
plot(x, y, 'g-o', 'MarkerFaceColor', 'y', 'LineWidth', 1.5);
xlabel('x'); ylabel('sin(x)');
title('Problem 7: Formatted Plot');
grid on;
Problem 8
x = logspace(0,2,100);
y = x.^2;
loglog(x, y, 'r-', 'LineWidth', 2);
xlabel('x (log scale)'); ylabel('y (log scale)');
title('Problem 8: loglog plot');
grid on;
Problem 9
x = 1:10;
y = 2*x + randn(1,10);
e = 0.5*ones(size(x));
errorbar(x, y, e, 'o-', 'MarkerSize', 8, 'LineWidth', 2);
xlabel('x'); ylabel('y');
title('Problem 9: Errorbar plot');
grid on;