I need to multipy two function handles and get function handle as a result.
e.g. :
u = #(x) x + 2;
v = #(x) 2*x + 1;
y = u * g;
How to do this?
A solution is
y = #(x)( u(x).*v(x) );
I dont know any other way.
Related
I have written MATLAB code to solve the following systems of differential equations.
with
where
and z2 = x2 + (1+a)x1
a = 2;
k = 1+a;
b = 3;
ca = 5;
cb = 2;
theta1t = 0:.1:10;
theta1 = ca*normpdf(theta1t-5);
theta2t = 0:.1:10;
theta2 = cb*ones(1,101);
h = 0.05;
t = 1:h:10;
y = zeros(2,length(t));
y(1,1) = 1; % <-- The initial value of y at time 1
y(2,1) = 0; % <-- The initial value of y' at time 1
f = #(t,y) [y(2)+interp1(theta1t,theta1,t,'spline')*y(1)*sin(y(2));
interp1(theta2t,theta2,t,'spline')*(y(2)^2)+y(1)-y(1)-y(1)-(1+a)*y(2)-k*(y(2)+(1+a)*y(1))];
for i=1:(length(t)-1) % At each step in the loop below, changed y(i) to y(:,i) to accommodate multi results
k1 = f( t(i) , y(:,i) );
k2 = f( t(i)+0.5*h, y(:,i)+0.5*h*k1);
k3 = f( t(i)+0.5*h, y(:,i)+0.5*h*k2);
k4 = f( t(i)+ h, y(:,i)+ h*k3);
y(:,i+1) = y(:,i) + (1/6)*(k1 + 2*k2 + 2*k3 + k4)*h;
end
plot(t,y(:,:),'r','LineWidth',2);
legend('RK4');
xlabel('Time')
ylabel('y')
Now what is want to do is define the interpolations/extrapolations outside the function definition like
theta1_interp = interp1(theta1t,theta1,t,'spline');
theta2_interp = interp1(theta2t,theta2,t,'spline');
f = #(t,y) [y(2)+theta1_interp*y(1)*sin(y(2));
theta2_interp*(y(2)^2)+y(1)-y(1)-y(1)-(1+a)*y(2)-k*(y(2)+(1+a)*y(1))];
But this gives the error
Please suggest a solution to this issue.
Note that in your original code:
f = #(t,y) [y(2)+interp1(theta1t,theta1,t,'spline')*y(1)*sin(y(2));
interp1(theta2t,theta2,t,'spline')*(y(2)^2)+y(1)-y(1)-y(1)-(1+a)*y(2)-k*(y(2)+(1+a)*y(1))];
the call to interp1 uses the input variable t. t inside this anonymous function is not the same as the t outside of it, where it is defined as a vector.
This means that, when you do
theta1_interp = interp1(theta1t,theta1,t,'spline');
then theta1_interp is a vector containing interpolated values for all your ts, not just one. One way around this is to create more anonymous functions:
theta1_interp = #(t) interp1(theta1t,theta1,t,'spline');
theta2_interp = #(t) interp1(theta2t,theta2,t,'spline');
f = #(t,y) [y(2)+theta1_interp(t)*y(1)*sin(y(2));
theta2_interp(t)*(y(2)^2)+y(1)-y(1)-y(1)-(1+a)*y(2)-k*(y(2)+(1+a)*y(1))];
Though this doesn't really improve your code in any way over the original.
The solution of the problem it is 1.732400451459101 for Simpson 1/3 Rule. Instead the solution that the program give me is 1.73239801
Can anyone help me out? Thanks in advance.
clc
clear
close all
f = #(x) sin(x);
a = 0.1;
g = a;
b = 2.4;
k = 19;
n = 2*k;
S = 0;
h = (b-a)/n;
for i=1:k
S=S+(h/3)*(f(a)+4*f(a+h)+f(a+2*h));
a=a+2*h;
end
fprintf('La integral se aproxima a: %0.8f \n',S)
syms x
y = sin(x);
InT = int(y,g,b);
InT = double(InT)
The error in approximating an integral by Composite Simpson's rule is:
which is about 1.7149e-07 in f(x)=sin(x) case, and that means the absolute error bound is 9.8990e-08, which is palatable to me.
Besides, here is an alternative to the code above:
f = #(x) sin(x);
[a,b,g,k]=deal(0.1,2.4,0.1,19);
[S,n,h]=deal(0,2*k,(b-a)/(2*k));
for i=1:k
S=S+(h/3)*(f(g)+4*f(g+h)+f(g+2*h));
g=g+2*h;
end
Or, we could just call:
f = #(x) sin(x);
[a,b,k]=deal(0.1,2.4,19);
Int = a:(b-a)/(2*k):b;
S=(b-a)/(6*k) * ( f(Int(1)) + 4*sum(f(Int(2:2:end-1))) ...
+ 2*sum(f(Int(3:2:end-2))) + f(Int(end)));
I use anonymous functions in my code. For example:
G = #(x) [norm(A*x-b);A'*(A*x-b)];
norm(Ax-b) is the objective function and A'*(Ax-b) the gradient.
Then,
Algo( G,varagin );
etc
What I would like to do is to define f with a loop:
n = 9;
k = 2;
t = 1 - x.^k;
f = 0;
for i=1:n
f = f + x(i,1)*prod(t(1:i-1));
end
grad_f = zeros(n,1);
for i0=1:n
s = t;
s(i0) = [];
for i=i0+1:n
grad_f(i0) = grad_f(i0) + x(i)*prod(s(1:i0-1));
end
grad_f(i0) = -k*x(i0)^(k-1)*grad_f(i0);
grad_f(i0) = grad_f(i0) + prod(t(1:i0-1));
end
Then I would like to do something like:
" G = #(x) [f,grad_f] "
Thanks a lot for your help!
Found the answer:
create F(x) and GRAD_F(x) as functions in matlab computing f and grad_f respectively.
Then:
G = #(x) [F(x);GRAD_F(x)];
Algo(G,varargin);
I need to write a for loop in matlab to solve a derivative using the forward difference method. The function to derive is 10+15x+20x^2 from 0 to 10 using steps of 0.25. I have tried using
h=.25;
x=[0:h:10];
y = 10+15*x+20*x.^2;
y(1) = 45; size(x)
for i=2:47,
y(i) = y(i-1) + h*(15+40*x);
end
I'd do like this, as a start,
h=.25;
x=[0:h:10];
y = 10+15*x+20*x.^2;
diff(y)./diff(x)
or, as alternative,
syms x;
y = 20.*x.^2 + 15.*x + 10;
dy = diff(y,1);
h=.25;
xx=[0:h:10];
res = subs(dy,xx);
Any simple way to define a discontinuous function (such as f(x)=5 if x>5, f(x)=6x if x<5) and evaluate it in a interval (such as [0 6]).
How about this:
f = #(x) 5*(x>5) + 6*x.*(x<5);
as in
t = 0:0.001:6;
f = #(x) 5*(x>5) + 6*x.*(x<5);
plot(t,f(t));
You may want to change your definition to make sure you define the case when x = 5, to be one of the following:
f = #(x) 5*(x>5) + 6*x.*(x<=5);
or
f = #(x) 5*(x>=5) + 6*x.*(x<5);