Assignment1byEricYU
-
Upload
eric-cire-yu -
Category
Documents
-
view
15 -
download
1
Transcript of Assignment1byEricYU
Assignment # 1
AMS 147
By Eric Yu
1) “Quadratic equation x^2+b*x+c = 0 has two roots. They are given by
r1 = (-b+sqrt(b^2-4*c))/2 and r2 = (-b-sqrt(b^2-4*c))/2.
Fix the value of c at c=1, plot r1 and r2 as functions of b for b in [2,3]. Plot two curves in
one figure. Use about 100 points to represent each curve.”
a. I simply set the value of c to 1. Set boundary for b and used about 100 points from
between 2, 3 for the curve. I then set r1 and r2 to its function and then plotted
them onto one figure.
b. c. The first curve (r1) slowly reached its peak value, while r2 quickly reached and
continued reaching the peak value. Since r2 is both –b and –square rooted, then it
was faster reaching its peak value than compared with r1.
2) “Plot the surface f(x,y)=sin(x^2+y^2)*exp(-sqrt(x^2+y^2)) for x in [-3,3] and y in [-3,3].
Use about 30 points in each dimension to represent the surface.”
a. I referred to the given codes we were to look at and studied them. On one of the
codes, it plotted a 3D plot similar to this problem. So I just modified the code and
inserted mine plotted it.
b. c. At first I didn’t know if this is the right graph because I had used it with wolfram
alpha to check it. Wolfram had a different graph, but I tried tweaking it but it still
came out with the same graph. The graph had 3 different axis: x,y, and f. The
various colors depict the depth of the graph and regions.
3) “Write a Matlab code to read in data from "data1.txt"
(in http://www.cse.ucsc.edu/~hongwang/Codes/Read_data) and plot the data. Plot the
fitting function f(x)=exp(-c*x)*cos(2*x) along with the data. Adjust the value of "c" to fit
the data. Use linear scales for both the X- and Y- axes.”
a. For this problem, I understood that we needed to load a file within the folder and
use that data to plot it. So I modified the other given code and used the write code
to load data1.txt. To graph and plot, it was as the same before, but this time, there
was also another function also implementing the data1.txt. I looked at the other
code and found out how to do it.
b. c. I wasn’t sure if I had done it right, but I managed to have 2 plotted data so I was
generally pleased with what I have. It was pretty difficult as I didn’t understand
the question at first.
Appendix – Codes
1) Problem 1 Codes
% Problem 1 % Quadratic equation x^2+b*x+c = 0 has two roots. They are given by %r1 = (-b+sqrt(b^2-4*c))/2 and r2 = (-b-sqrt(b^2-4*c))/2. %Fix the value of c at c=1, plot r1 and r2 as functions of b for b in %[2,3]. %Plot two curves in one figure. Use about 100 points to represent each %curve.
%by Eric Yu % clear all; clc; %
c = 1;
dx = .001;
b = [2 : dx : 3];
r1 = (-b + sqrt(b.^2-4*c))/2;
r2 = (-b - sqrt(b.^2-4*c))/2;
plot(b, r1, b, r2);
2) Problem 2 Codes
% Problem 2 %Plot the surface f(x,y)=sin(x^2+y^2)*exp(-sqrt(x^2+y^2)) for x in [-3,3] %and y in [-3,3]. Use about 30 points in each dimension to represent %the surface. % % %by Eric Yu %
clear all; clc; %
m=31; n=31; dx=6/(n-1); dy=6/(m-1); x1=[-3:dx:3]; y1=[-3:dy:3];
[x,y]=meshgrid(x1,y1);
f = sin(x.^2+y.^2)*exp(-sqrt(x.^2+y.^2));
h=surf(x,y,f);
3) Problem 3 Codes
%Problem 3 % %Write a Matlab code to read in data from "data1.txt" %and plot the data. Plot the fitting function f(x)=exp(-c*x)*cos(2*x) %along with the data. Adjust the value of "c" to fit the data. %Use linear scales for both the X- and Y- axes. % % %by Eric Yu %
clear figure(1) clf reset axes('position',[0.15,0.13,0.75,0.75])
load -ascii data1.txt x = data1(:,1); y = data1(:,2); x1 = [0:.2:10]; c = 0.03; f =exp(-c*x1).*cos(2*x1);
semilogy(x,y,'b-s','markerfacecolor','b') hold on semilogy (x,f, 'b-s','markerfacecolor','b') set(gca,'fontsize',10) axis([0,11,-1,2]) set(gca,'xtick',[0:.2:11]) set(gca,'ytick',[0:.5:2])