Uploaded by waqasaliazhar

lab 1

advertisement
LAB 1 - Your Name - MAT 275
Exercise 1
NOTE: Please suppress output - i.e., use a semicolon ';' at the end of any commands for which the output is not
necessary to answer the question. Delete these notes before turning in.
Define input variable theta as discretized row vector (i.e. array)
theta =[0, pi/7, pi/5, pi/4, 5*pi/4, 6*pi/5, 7*pi/6];
Define radius
r=7;
Define x and y in terms of theta and r
x=r*cos(theta);
y=r*sin(theta);
Check that x and y satisfy the equation of a circle
r1=sqrt(x.^2+y.^2)
Explain results here. Yes for each value of x,y we get sqrt(x^2+y^2)=7.
Exercise 2
Define t-vector
t=linspace(4, 20, 44);
Define y-vector
y= exp(t/20) .* cos(2*t) ./ (0.6*t.^3 + 11);
Part (a)
Plot results (should have 3 plots total)
figure;
plot(t, y, 'k'); % 'k' for black line
title('y = e^{t/20} cos(2t) /(0.6t^3 + 11)');
xlabel('t');
ylabel('y');
grid on;
1
Part (b)
Plot results as data points only and as data points with line.
figure %creates another figure window
plot(t, y, 'ko-', 'MarkerFaceColor', 'k'); % 'ko-' for black circles and line
title('Data Points and Line: y = e^{t/20} cos(2t) /(0.6t^3 + 11)');
xlabel('t');
ylabel('y');
grid on;
2
Exercise 3
Create t-vector (choose enough elements so that plot is smooth!)
t = linspace(0, 20, 1000);
Define x, y, x components in terms of t
x = 9 * cos(2 * t);
y = 9 * sin(2 * t);
z = 7 * t;
Plot results
figure;
plot3(x, y, z);
grid on;
% Label the axes
xlabel('X');
ylabel('Y');
zlabel('Z');
% Set a title for the plot
title('Circular Helix: x = 9*cos(2t), y = 9*sin(2t), z = 7t');
3
% Adjust the view angle if necessary
view(3); % 3D view
NOTE: if graph does not look smooth use more elements in your t-vector -- i.e. use smaller stepsize between
elements. Delete these notes before submission.
Exercise 4
Define input variable as vector
x= linspace(-pi/6, pi/6, 1000);
Define y and z
y = sin(6 * x);
z = 6 * x - 36 * x.^3;
Plot results
figure;
plot(x, y, 'r', x, z, 'b--');
% Add a grid to the plot
grid on;
4
% Set axis limits to the range of the data
axis tight;
% Label the x and y axes
xlabel('x');
ylabel('y and z');
% Add a legend to differentiate the curves
legend('y = sin(6x)', 'z = 6x - 36x^3');
% Title for the plot
title('Comparison of y = sin(6x) and Taylor Polynomial Approximation z = 6x
- 36x^3');
Exercise 5
NOTE: you must create the M-file ex5.m separately and invoke it here in your livescript file. Delete these notes
before submission.
type ex5.m
x = linspace(0, 7, 1000); % Use enough points for smooth curves
% Define the function that computes the solution with the given C
function y = f(x, C)
y = 21 * x.^2 / 2 - 14 * x.^3 / 3 + 15 * sin(x) + C;
5
end
% Compute the solutions with different initial conditions
y1 = f(x, 220);
y2 = f(x, 360);
y3 = f(x, 540);
% Plot the three solutions with different line-styles and colors
hold on;
plot(x, y1, 'r-', 'LineWidth', 2, 'DisplayName', 'C = 220');
plot(x, y2, 'g--', 'LineWidth', 2, 'DisplayName', 'C = 360');
plot(x, y3, 'b:', 'LineWidth', 2, 'DisplayName', 'C = 540');
hold off;
% Add a title to the plot
title('Solutions to dy/dx = 21x - 14x^2 + 15 cos(x)');
% Add a legend
legend('Location', 'best');
% Label the x and y axes
xlabel('x');
ylabel('y');
% Display the plot
grid on;
Run your M-file--i.e. execute the M-file
run 'ex5.m'
Error: File: ex5.m Line: 9 Column: 1
Function definitions in a script must appear at the end of the file.
Move all statements after the "f" function definition to before the first local function definition.
Error in run (line 91)
evalin('caller', strcat(script, ';'));
Exercise 6
Part (a)
Define g as anonymous function
% Define g(x, y) as an anonymous function
g = @(x, y) x^6 / y^3 + cos(4*x*exp(5*y) / (x^2 + 3));
% Evaluate g(-1, -8)
result_a = g(-1, -8);
Evaluate g at the given values of x and y
Part (b)
NOTE: You need to create a separate M-file (g.m) and invoke it in the main file, here. Erase these notes upon
submission.
Clear the function g out of the workspace
% Clear the anonymous function defined in part (a)
6
clear g;
% Evaluate g(-1, -8) using the M-file
result_b = g(-1, -8);
7
Download