I am trying to evaluate a convolution integral using the symbolic int() function and instead of returning a useful answer, my program is returning the integral itself. Here is my code
clc; clear;
pkg load symbolic
syms t tau Wn % Declare symbolic variables
f = tau^2 * sin( Wn *(t-tau) );
convolution = int( f, tau, [0 t] ); % Specify tau as the integration variable
% and integration limits are from 0 to t
pretty(convolution)
The code runs, but does not return something useful. Instead of returning an answer, it returns this:
t
⌠
⎮ 2
⎮ τ ⋅sin(Wn⋅t - Wn⋅τ) dτ
⌡
0
i.e. the original integral with the function inside of it.
I've tried troubleshooting the problem in the following ways:
Copy/pasting example code from the Octave int() help page, this works and evaluates the definite integral
Changing the integration syntax from 0, t to [0, t] this changes nothing
Making variable f and storing the function there, instead of the function being inside of int(). This changes nothing
I know the symbolic package is working, because the example code returns the correct definite integral.
Thanks.
You can use eval in order to "evaluate" (duh) the integral. E.g. with your code above, I get:
octave:9> eval( convolution )
ans = (sym)
⎧ 2
⎪t 2⋅cos(Wn⋅t) 2
⎪── + ─────────── - ─── for Wn > -∞ ∧ Wn < ∞ ∧ Wn ≠ 0
⎨Wn 3 3
⎪ Wn Wn
⎪
⎩ 0 otherwise
Note that if you actually 'define' some of those symbols on the workspace, then these get taken into account:
octave:10> Wn = 1; % or preferably `sym('1')` ...
octave:11> eval( convolution )
ans = (sym)
2
t + 2⋅cos(t) - 2
Related
I'm trying to take the definite integral from -1 to 1 of a function with respect to x. The function has variables a, b, c, d, and x, all of which I've defined as syms variables. I'm trying to keep a, b, c, d in my final integral because I'll later be differentiating with respect to each one for an optimization problem. Here's the current code that I have:
syms f(x);
syms a b c d;
f(x)= (exp(x)-a*(1/sqrt(2))-b*(sqrt(3/2)*x)-c((sqrt(45/8))*(x^2-(1/3)))+d((sqrt(175/8))*((x^3)-(3/5)*(x))))^2;
integral = int(f, x, [-1 1]);
disp(integral);
Similar code worked when I tried it using only variables x and y for a smaller function. However, when I try this code, I get:
Error using sym/subsindex (line 825) Invalid indexing or function
definition. Indexing must follow MATLAB indexing. Function arguments
must be symbolic variables, and function body must be sym expression.
Error in sym/subsref (line 870)
R_tilde = builtin('subsref',L_tilde,Idx);
Error in HW11 (line 4)
f(x)= (exp(x)-a*(1/sqrt(2))-b*(sqrt(3/2)x)-c((sqrt(45/8))(x^2-(1/3)))+d((sqrt(175/8))((x^3)-(3/5)(x))))^2;
I'm pretty new to symbolic functions and syms variables in MATLAB, why is MATLAB rejecting this code? The similar code that I tried that worked was:
syms f(x);
syms y;
f(x) = (x^2) + y;
integral = int(f, x, [0 3]);
disp(integral);
As mentioned in the comment by Adam, you probably forgot to add a multiplication operator * after the the c and d, so when you write c(...) and d(...) MATLAB treats these as indexing of an array but you cannot index arrays with symbolic variables or expressions. You need to change it to c*(...) and d*(...).
Replace:
f(x)= (exp(x)-a*(1/sqrt(2))-b*(sqrt(3/2)*x)-c((sqrt(45/8))*(x^2-(1/3)))+d((sqrt(175/8))*((x^3)-(3/5)*(x))))^2;
With:
f(x)= (exp(x)-a*(1/sqrt(2))-b*(sqrt(3/2)*x)-c*((sqrt(45/8))*(x^2-(1/3)))+d*((sqrt(175/8))*((x^3)-(3/5)*(x))))^2;
MATLAB gives me the same expression back.
Here's my code
syms tau alpha phi
f = sign(alpha*cos(phi))*(abs(alpha*cos(phi)) - 2.5*(alpha*cos(phi))^2);
F=f*sin(phi);
int(F,phi, [pi/2, acos(tau/alpha)])
I did plug in the values of variables tau and alpha but it still gives me the same expression back. Anyone know how to solve it? Or some other numerical method which could give me answer in these symbols? Will wolfram-mathematica help?
Mathematica
Integrate[(Sign[alpha*Cos[phi]]*(Abs[alpha*Cos[phi]] - 5/2*
(alpha*Cos[phi])^2))*Sin[phi], {phi, Pi/2, ArcCos[tau/alpha]}]
(* ConditionalExpression[-(tau^2/(2 alpha)) + (5 alpha^3 Abs[tau]^3)/
(6 Abs[alpha]^4), ArcCos[tau/alpha] \[Element] Reals] *)
Thus the result is the expression inside the ConditionalExpression if the ArcCos[tau/alpha] is an element of the reals.
If you could specify something like -1<=tau/alpha<=1 && 0<=tau then it can provide an even simpler result, (tau^2 (-3 + 5 tau))/(6 alpha)
Please verify this before you depend on it.
Something like
syms tau alpha phi
f = sign(alpha*cos(phi))*(abs(alpha*cos(phi)) - 2.5*(alpha*cos(phi))^2);
will just generate another symbolic variable. Your goal however is to specify a symbolic function and for that you have to specify the function arguments:
syms tau alpha phi
f(tau,alpha,phi) = sign(alpha*cos(phi))*(abs(alpha*cos(phi)) - 2.5*(alpha*cos(phi))^2);
F(tau,alpha,phi) =f*sin(phi);
Then you can calculate the integral with
R = int(F,phi, [pi/2, acos(tau/alpha)])
Since your integral is dependent on tau and alpha, R is again a symbolic function R(tau,alpha).
Alternatively you could just specify the arguments at the end, e.g.
syms tau alpha phi
f = sign(alpha*cos(phi))*(abs(alpha*cos(phi)) - 2.5*(alpha*cos(phi))^2);
F=f*sin(phi);
R(tau,alpha) = int(F,phi, [pi/2, acos(tau/alpha)])
But I personally find that less clean.
Note that you could also write F directly as
F(tau,alpha,phi) = sin(phi)*(sign(alpha*cos(phi))*(abs(alpha*cos(phi)) - 2.5*(alpha*cos(phi))^2));
I have tried many times taking different initial values of c. I did not get optimum values of c using function fminsearch. I have found more error between simulated and measured values of sigma. Please help me: how can optimize my function?
clc
close all
clear all
syms c sigma est_bio mea_bio
sigma=[-15.1015 -13.7879 -13.0576 -12.7818 -12.3839 -11.7587 -11.1756 -10.6291 -9.9176];
mea_bio=[0.181 0.204 0.529 0.632 1.059 1.533 1.934 1.977 1.861];
%%% create model function q with parameters
q = #(c, mea_bio) ((c(1)/(-2*c(2))).*(1-exp(2*c(2).*mea_bio))+c(3).*exp(2*c(2).*mea_bio))
%// create the desired error-functions for minimization
h = #(c) sum((q(c, mea_bio) - sigma).^2); %// default minimizaton function
c= [-.05 -.0500 -.0500]; % an initial guess
[p_fit_e, r_e] = fminsearch(h, c) % Optimize
est_q = ((c(1)/(-2*c(2))).*(1-exp(2*c(2).*mea_bio))+c(3).*exp(2*c(2).*mea_bio))
err=est_q-sigma
Why do you define c, sigma est_bio and mea_bio as symbolic variables? It doesn't make sense. This is a numerical optimization, no need to involve symbolic computations. Remove that line from your code and it should work:
syms c sigma est_bio mea_bio
I have the following anonymous function:
f = #(x)x^2+2*x+1
I'm using this so that I use it in the following way:
f(0) = 1
But what if I want to find the derivative of such a function while still keeping it's anonymous function capability? I've tried doing the following but it doesn't work:
f1 = #(x)diff(f(x))
but this just returns
[]
Any thoughts on how to accomplish this?
Of course I could manually do this in 3 seconds but that's not the point...
If you have symbolic math toolbox, you can use symbolic functions to achieve the desired as follows:
syms x
myFun=x^2+2*x+1;
f=symfun(myFun,x);
f1=symfun(diff(f),x);
%Check the values
f(2)
f1(2)
You should get 9 and 6 as answers.
When you do diff of a vector of n elements it just outputs another vector of n-1 elements with the consecutive differences.. so when you put a 1 element vector you get an empty one.
A way to go would be to decide an epsilon and use the Newton's difference quotient:
epsilon = 1e-10;
f = #(x) x^2+2*x+1;
f1 = #(x) (f(x+epsilon) - f(x)) / epsilon;
or just do the math and write down the formula:
f1 = #(x) 2*x+2;
http://en.wikipedia.org/wiki/Numerical_differentiation
#jollypianoman this works to me. Actually you need to say that the symfun has to be evaluate using eval command, then you get all the features of an anonymous function. the best is to read the example below...
clear
N0=1;N1=5;
N=#(t) N0+N1*sin(t);
syms t
Ndot=symfun(diff(N(t)),t);
Ndot_t=#(t) eval(Ndot);
Ndot_t(0)
ans = 5
Ndot_t(1)
ans = 2.7015
[tstop] = fsolve(Ndot_t,pi/3)
tstop =
1.5708
cheers,
AP
We have an equation similar to the Fredholm integral equation of second kind.
To solve this equation we have been given an iterative solution that is guaranteed to converge for our specific equation. Now our only problem consists in implementing this iterative prodedure in MATLAB.
For now, the problematic part of our code looks like this:
function delta = delta(x,a,P,H,E,c,c0,w)
delt = #(x)delta_a(x,a,P,H,E,c0,w);
for i=1:500
delt = #(x)delt(x) - 1/E.*integral(#(xi)((c(1)-c(2)*delt(xi))*ms(xi,x,a,P,H,w)),0,a-0.001);
end
delta=delt;
end
delta_a is a function of x, and represent the initial value of the iteration. ms is a function of x and xi.
As you might see we want delt to depend on both x (before the integral) and xi (inside of the integral) in the iteration. Unfortunately this way of writing the code (with the function handle) does not give us a numerical value, as we wish. We can't either write delt as two different functions, one of x and one of xi, since xi is not defined (until integral defines it). So, how can we make sure that delt depends on xi inside of the integral, and still get a numerical value out of the iteration?
Do any of you have any suggestions to how we might solve this?
Using numerical integration
Explanation of the input parameters: x is a vector of numerical values, all the rest are constants. A problem with my code is that the input parameter x is not being used (I guess this means that x is being treated as a symbol).
It looks like you can do a nesting of anonymous functions in MATLAB:
f =
#(x)2*x
>> ff = #(x) f(f(x))
ff =
#(x)f(f(x))
>> ff(2)
ans =
8
>> f = ff;
>> f(2)
ans =
8
Also it is possible to rebind the pointers to the functions.
Thus, you can set up your iteration like
delta_old = #(x) delta_a(x)
for i=1:500
delta_new = #(x) delta_old(x) - integral(#(xi),delta_old(xi))
delta_old = delta_new
end
plus the inclusion of your parameters...
You may want to consider to solve a discretized version of your problem.
Let K be the matrix which discretizes your Fredholm kernel k(t,s), e.g.
K(i,j) = int_a^b K(x_i, s) l_j(s) ds
where l_j(s) is, for instance, the j-th lagrange interpolant associated to the interpolation nodes (x_i) = x_1,x_2,...,x_n.
Then, solving your Picard iterations is as simple as doing
phi_n+1 = f + K*phi_n
i.e.
for i = 1:N
phi = f + K*phi
end
where phi_n and f are the nodal values of phi and f on the (x_i).