Matlab files used

1.
problem1.m
2.
problem2.m
3.
ssolve.m
4.
problem_4_driver.m

Math 513 HW1 spring 2014, University of Wisconsin, Madison

Nasser M. Abbasi

January 25, 2020

Contents

1 Problem 1
2 Problem 2
3 Problem 3
4 Problem 4

1.
problems as0.pdf

1 Problem 1

Question: Generate a plot of the function \(\sin \) on the interval [−1, 1] with an appropriate title. Turn in the figure together with the commands you used to generate the figure.

Answer

 
%HW1, problem 1, MATH 513, Univ. Of Wisconsin, Madison. SPring 2014 
%by Nasser M. Abbasi 
 
close all; clear all; 
 
x    = linspace(-1,1); %default 100 points 
fig1 = figure(1);clf; 
 
set(fig1,'defaulttextinterpreter','latex'); 
plot(x,sin(x)) 
xlabel('$x$'); 
ylabel('$\sin(x)$'); 
title('plot of $\sin(x)$ vs. $x$'); 
grid on;

pict
Figure 1:problem one plot

2 Problem 2

Question: Find the solution to the following 20-by-20 system of equations. Turn in your code and the output \[ \begin{pmatrix} 1 & & \cdots & 1 & 0 \\ & 1 & & & \\ \vdots & & \ddots & & \vdots \\ & & & 1 & 0 \\ 1 & \cdots & & & 1 \end{pmatrix} x = \begin{pmatrix} 17 \\ 0 \\ \vdots \\ \\ 0 \end{pmatrix} \]

[Note: All entries in the matrix not explicitly mentioned are meant to be zero]

Answer

 
%HW1, problem 2, MATH 513, Univ. Of Wisconsin, Madison. SPring 2014 
%by Nasser M. Abbasi 
close all; clear all; 
 
N      = 20; 
A      = zeros(N); 
A(:,1) = 1; 
A(logical(eye(size(A)))) = 1; 
A(1,1:end-1)   = 1; 
A(end,:)       = 1; 
 
b    = zeros(N,1); 
b(1) = 17; 
 
x = A\b; 
 
fprintf('solution is \n');  x(:) 
%------------------------------- 
   -1.0000 
    1.0000 
    1.0000 
    1.0000 
    1.0000 
    1.0000 
    1.0000 
    1.0000 
    1.0000 
    1.0000 
    1.0000 
    1.0000 
    1.0000 
    1.0000 
    1.0000 
    1.0000 
    1.0000 
    1.0000 
    1.0000 
  -17.0000

3 Problem 3

Question: Create a Matlab function called ssolve that:

1.
Accepts three parameters, \(a\), \(b\), and \(n\), in that order.
2.
Returns the solution to the following \(n\) by \(n\) system

\[ \begin{pmatrix} 1 & a & & & \\ & 1 & & & \\ & & \ddots & & \\ & & & 1 & \\ b & b & \cdots & b & 1 \end{pmatrix} x = \begin{pmatrix} 1 \\ 2 \\ \vdots \\ n-2 \\ 0 \\ n \end{pmatrix} \]

[Note: All entries in the matrix not explicitly mentioned are meant to be zero]

Answer

 
function x = ssolve( a,b,n ) 
%ssolve(a,b,n) 
%HW1, problem 3, MATH 513, Univ. Of Wisconsin, Madison. Spring 2014 
%by Nasser M. Abbasi 
 
A              = zeros(n); 
A(1,2)         = a; 
A(end,1:end-1) = b; 
 
A(logical(eye(size(A)))) = 1; 
 
v        = 1:n; 
v(end-1) = 0; 
 
x = A\v(:); 
end

4 Problem 4

Question: Turn in a printout of the function ssolve and plots of the vectors

1.
ssolve(10, 0.1, 30)
2.
ssolve(2, -0.05, 50)

with the appropriate titles. Also turn in the code that generated these figures.

Answer

EDU>> x = ssolve(10,.1,30) 
x = 
  -19.0000 
    2.0000 
    3.0000 
    4.0000 
    5.0000 
    6.0000 
    7.0000 
    8.0000 
    9.0000 
   10.0000 
   11.0000 
   12.0000 
   13.0000 
   14.0000 
   15.0000 
   16.0000 
   17.0000 
   18.0000 
   19.0000 
   20.0000 
   21.0000 
   22.0000 
   23.0000 
   24.0000 
   25.0000 
   26.0000 
   27.0000 
   28.0000 
         0 
   -8.6000
EDU>> x = ssolve(2,-0.05,50) 
x = 
 
   -3.0000 
    2.0000 
    3.0000 
    4.0000 
    5.0000 
    6.0000 
    7.0000 
    8.0000 
    9.0000 
   10.0000 
   11.0000 
   12.0000 
   13.0000 
   14.0000 
   15.0000 
   16.0000 
   17.0000 
   18.0000 
   19.0000 
   20.0000 
   21.0000 
   22.0000 
   23.0000 
   24.0000 
   25.0000 
   26.0000 
   27.0000 
   28.0000 
   29.0000 
   30.0000 
   31.0000 
   32.0000 
   33.0000 
   34.0000 
   35.0000 
   36.0000 
   37.0000 
   38.0000 
   39.0000 
   40.0000 
   41.0000 
   42.0000 
   43.0000 
   44.0000 
   45.0000 
   46.0000 
   47.0000 
   48.0000 
         0 
  108.6000

Now the two solutions are plotted.

 
%script to plot the solutions. filename: problem_4_driver.m 
%HW1, problem 4, MATH 513, Univ. Of Wisconsin, Madison. Spring 2014 
%by Nasser M. Abbasi 
 
close all; clear all; 
x1 = ssolve(10,.1,30); 
x2 = ssolve(2,-0.05,50); 
 
fig1 = figure(1);clf; 
set(0,'DefaultAxesFontName', 'Times New Roman') 
set(0,'DefaultAxesFontSize', 10) 
subplot(1,2,1); 
 
set(fig1,'defaulttextinterpreter','latex'); 
plot(x1,'-'); 
xlabel('$x$'); 
ylabel('$y(x)$'); 
title('solution of $ssolve(10,.1,30)$'); 
grid on; 
 
subplot(1,2,2); 
 
plot(x2,'-'); 
xlabel('$x$'); 
ylabel('$y(x)$'); 
title('solution of $ssolve(2,-0.05,50)$'); 
 
grid on;

pict
Figure 2:problem four plot