You are still thinking that you are using some other language, where you need to declare variables as doubles.
This line does not do what you think:
double a; double b; double c;
In fact, it does not even create variables with that name. What did it do?
So when I tried whos there after having used the line you had written, it shows that NOTHING useful was done. No variables were created. What did those calls do?
Here, we see that call takes the character 'a', converts it to its ascii equivalent (97), then puts the result in the variable ans, effectively dumping it in the bit bucket.
In the case where you want to define those variables as unknown constants, which is what it looks like you want to do? I say that because you never did assign any value to those unknown constants. If so, then you might have done this:
So you can see that now, a,b, and c now exist, and are symbolic variables. They are now effectively unknown constants. syms is the one case where you will want to declare the variables you use as symbolic. Everything else is a double by default.
So p is a symbolic polynomial, with unknown coefficients a,b,c. Now we can form the expression you had, and integrate it over the interval [0,1], given the unknown constants a,b,c.
I = int((exp(x) - p).^2,x,[0,1])
Note that the variable e shown in that expression is exp(1).
See that integral cannot be used with symbolic parameters. You needed to use int for that.
Alternatively, suppose a,b, and c had actual numeric values? Now I would suggest you learn to use function handles.
P = @(X) a*X.^2 + b*X + c
P is now a function handle. It encapsulates the current values of a, b and c. You can pass p around as a variable, and then use it as a function. I used a new variable X here to not confuse you.
f = @(X) (exp(X) - P(X)).^2
f is also a function handle. It knows what P is, and since P already knows the values of a, b, and c, everything will work. We can now form the integral, as
We have a simple numerical value for this result.
Finally, I can imagine where you are going with this little task. Suppose we wanted to find the optimzl values of a, b, anc c to best approximate exp(x) over the interval [0,1]? Now we might do this:
p = @(x,a,b,c) a*x.^2 + b*x + c
Hee a,b,c,x are all expected to be variables passed into the function handle p. They are all currently unknowns.
f = @(x,a,b,c) (exp(x) - p(x,a,b,c)).^2;
I = @(a,b,c) integral(@(x) f(x,a,b,c),0,1);
At this point, NO integral has yet been computed. All I have done is to set up the functions to be used. So finally, minimize the integral, over the unkown constants a,b,c. We can use fminsearch for this.
[abcopt,minI] = fminsearch(@(abc) I(abc(1),abc(2),abc(3)), abcstart)
How well did we do? minI is pretty small, so I expect we did well.
title 'Approximating exp(x) with a quadratic polynomial'
The approximation is seen to be quite good, as the two curves are almost impossible to distinguish from each other.
fplot(@(x) exp(x) - p(x,abcopt(1),abcopt(2),abcopt(3)),[0,1])
title 'Error in approx of exp(x) with a quadratic polynomial'
As one would expect, the difference between the two should look very much like a cubic polynomial, with three zero crossings on the interval.
Your side question:
Why do you need to use .^ to square a matrix or vector, if you intend to square the elements?
Suppose we create the matrix A as:
Now what happens when we square A?
This is equivalent to writing A*A, where this is a MATRIX multiplication. If your goal is to square the elements of A, you need to write
The same thing applies to vectors. But if V is a vector, and you wanted to square the elements of V?
If I write V*V now, MATLAB will generate an error, because V*V does not conform for a MATRIX multiplication, thus a dot product. Squaring the elements of V requires we write