Lab Solutions


Topics in this lab

Introduction

clear all;
close all;

Back to the top

Problem #1

xdata = [-3.2; 5.4];
ydata = [1.2; -4.3];

V = vander(xdata);
a = V\ydata;
fprintf('Slope is       %16.8f\n',a(1));
fprintf('y-intercept is %16.8f\n',a(2));
xs = linspace(-4,6,20);
ys = a(1)*xs + a(2);

plot(xs,ys,'r','linewidth',2);
hold on;
plot(xdata,ydata,'k.','markersize',30);
plot(0,a(2),'b.','markersize',30);
plot([0 0],get(gca,'ylim'),'k--');
xlim = get(gca,'xlim');
plot(xlim,[a(2) a(2)],'k--');
snapnow;
Slope is            -0.63953488
y-intercept is      -0.84651163

Back to the top

Problem #2

close all;
xdata = [-3.2; 1.2; 6.1];
ydata = [4.5; 6.1; -3.4];

V = vander(xdata);
a = V\ydata;
xmax = -a(2)/(2*a(1));
ymax = polyval(a,xmax);
fprintf('Maximum value is at x = %16.8f\n',xmax);
fprintf('Maximum value is    y = %16.8f\n',ymax);

xs = linspace(-4,7,100);
ys = polyval(a,xs);

plot(xs,ys,'r','linewidth',2);
hold on;
plot(xdata,ydata,'k.','markersize',30);
ylim = get(gca,'ylim');
plot([xmax xmax],ylim,'k--');
plot(xmax,ymax,'b.','markersize',30);
xlim = get(gca,'ylim');
plot(xlim,[ymax,ymax],'k--');
axis([-4 7 ylim]);
snapnow;
Maximum value is at x =      -0.26559226
Maximum value is    y =       6.63177314

Back to lab exercises.

Back to the top

Get the code

Do you want to try the above code fragments on your own? Download the Matlab script that produces this page here. (lab_9_solns.m)

Powered by MathJax

Published with MATLAB® 8.2