I have to write a C program for a class; due at 9 pm tonight.
I have absolutely no idea how to get started on this.
I've worked with arrays and matrices to some degree but I don't have much experience.
I'm not really sure what it is asking me to do or how to do it. Please help!
This is the prompt:
/*
In this homework you will write three functions: one to multiply a
matrix by a vector, another to compute the Euclidean norm of a
vector, and a function to copy one vector into another. Then you
will use these functions to compute the values of the iteration:
x(k + 1) = A x(k), k = 0, 1, 2, ...
where x(k) are n-dimensional vectors and A is an n-by-n matrix.
In your program (hw8.c) write a function called mat_vec_mult whose
prototype is:
void
mat_vec_mult(double A[], int m, int n,
double x[], int nx,
double y[], int ny);
This function will take three arrays (A, x, y) as arguments as well
as a number of integers. The function mat_vec_mult should then
perform the matrix-vector product:
y = Ax.
If A is m-by-n then x should be an n-dimensional vector and y should
be m-dimensional. The vectors x and y should be stored directly as
n- and m-dimensional C arrays (not sparse!). The matrix A however
will not be stored as a two-dimensional C array. Instead, we will
stack up all the columns in a single one-dimensional array (note
that double A[] is the first parameter of mat_vec_mult!) For
example, if A is the 3-by-4 matrix
A = [1 2 3 4
5 6 7 8
9 10 11 12]
the corresponding array A is
{1, 5, 9, 2, 6, 10, 3, 7, 11, 4, 8, 12}.
That is, the one-dimensional array A contains all elements of the
matrix A stored column by column.
In mat_vec_mult, the integers m and n correspond to the dimensions
of the matrix A and nx and ny to the dimensions of the vectors x and
y, respectively. The function mat_vec_mult must check if all
dimensions are compatible and issue a warning but not interrrupt
your program in case they are not compatible.
As a simple example, if
A = [1 2 3 4
5 6 7 8
9 10 11 12]
x = {1, -1, 0, 1}
then
y = {3, 7, 11}
Write also a function with prototype:
double
vec_copy(double x[], int nx, double y[], int ny);
that checks whether nx and ny are the same and issue a warning if
they are not, then copies the nx-dimensional vector stored in the
array x into the nx- dimensional vector stored in the array y.
Finally write a function with prototype:
double
vec_norm(double x[], int nx);
to compute the Euclidean norm of a nx-dimensional vector stored in
the array x.
Now in the main() function of your program compute the iteration
x(k + 1) = A x(k), k = 0, 1, 2, ...
Use the function mat_vec_mult() you wrote to compute the product of
the matrix A by the vector x(k) on each iteration. A single
one-dimensional C array x should be used to hold the values of x(k)
as the iteration progresses. During an interation, use other arrays
if necessary to perform the computations and the function vec_copy()
to transfer the results of the computation to the array x at the end
of the iteration.
Use the function vec_norm() to compute the Euclidean norm of x(k) at
each iteration. If this norm falls below 1E-4 you should print a
message indicating that and exit the program.
You should also interrupt your program if after k reaches 100 the
norm of x(k) is not yet below 1E-4. Print a warning indicating that
on exit.
Print the value of the initial vector x(0) and each subsequent x(k)
on the screen. Print also the Euclidean norm of x(k) at each
iteration.
Use as data the 3-by-3 matrix
A = [ 1 1 0
1 -1 2
-1 -1 0 ]
and 3-dimensional vector
x(0) = {1, -1, 2}
For the above data your program should produce output that looks
like:
.................................................. ......................
iacs5.ucsd.edu% ./a.out
x(0) = {1, -1, 2}
||x(0)|| = 2.44949
x(1) = {0, 6, 0}
||x(1)|| = 6
x(2) = {6, -6, -6}
||x(2)|| = 10.3923
x(3) = {0, 0, 0}
||x(3)|| = 0
||x(3)|| < 1e-4.
Exiting...
iacs5.ucsd.edu%
.................................................. ......................
*/
This is what I have so far:
Code:#include<stdio.h> #include<math.h> #include<string.h> void main() { mat_vec_mult(double A[], int m, int n, double x[], int nx, double y[], int ny); }
Thank you so much!