multiply two functions with each other (matlab) - matlab

I'm trying to multiply two functions with each other and plot the result. but I got an empty figure!
the both functions work fine if I plot each one like this:
plot(x,ramp3(x))
hold on
plot(x,unitStep(3-x))
this is my code:
clear all
clc
x=0:0.001:20;
y3=#(x) ramp3(x).*unitStep(3-x);
plot(x,y3)
axis([-4 4 -2 2])

When you use the #(...) ... syntax, MATLAB generates an anonymous function, treating the argument list within the parentheses as inputs. The scope of those variables is limited to the anonymous function. The function is not evaluated until you call it and pass in valid inputs.
In your case y3 is a function handle. You need to use something like plot(x,y3(x)) to actually evaluate the function y3 at the points in your array x.

Related

Nested call to integral fails

Try this piece of code, which works fine.
a=1;b=2;
% A two-variate function
f2= #(x,y) x+y;
derivedF2=#(x) integral(#(y) f2(x,y), a,b);
% Test the evaluation of the derived function handle
derivedF2(0);
% Test the integration of the derived function handle
% integralVal=integral(derivedF2,a,b);
% integralVal=integral(#(x) derivedF2(x),a,b);
% Test plotting of the derived function handle
figure(11);
ezplot(derivedF2);
But if you uncomment the lines starting with integralVal. The code breaks.
Apparently, the derived function handle does not support integration operation, or have I missed something?
Short answer: you should add the 'ArrayValued' option:
integralVal=integral(derivedF2,a,b, 'ArrayValued', true);
Explanation
You should read your error message:
Output of the function must be the same size as the input. If FUN is an array-valued integrand, set the 'ArrayValued'
option to true.
Because derivedF2 is evaluated in a vectorised way, i.e. it evaluates f at different y coordinates at once by supplying a y vector instead of a single scalar, MATLAB is unable to evaluate the outer integral in a vectorised way too. Therefore you should add the 'ArrayValued' option to the outer integral, i.e.:
integralVal=integral(derivedF2,a,b, 'ArrayValued', true);
Note that ezplot also generate the following related warning:
Warning: Function failed to evaluate on array inputs; vectorizing the function may speed up its evaluation and avoid the
need to loop over array elements.
Note that the problem is purely related to nested calls to integral, also the following code will result in the same error:
integralVal=integral(#(x) integral(#(y) f2(x,y), a,b),a,b);
What is an Array Valued function?
... a function that accepts a scalar input and returns a vector, matrix, or N-D array output.
So, #(y) f2(x, y) is an array valued function if x is an array, i.e. it returns an array for a scalar input of y.
Two possibilities exist to avoid array valued problem:
Avoid that #(y) f2(x, y) is an array valued function, i.e. avoid that x is an array. This can be done by indicating that derivedF2 is an array valued function as elaborated above, although - strictly speaking - it is not an array valued function, i.e. the integral should have the same number of outputs and inputs. However, it uses internally an array valued function, i.e. #(x) f2(x, y) is an array valued function as Matlab evaluates by default the integrand in a vectorised way, i.e. it uses a vector for y.
Tell Matlab that #(y) f2(x, y) is an array valued function:
derivedF2=#(x) integral(#(y) f2(x,y), a,b, 'ArrayValued', true);
This may be a more intuitive approach, but is slower as the internal integral is called much often than the external integral.
An alternative interpretation of Array Valued is that you tell matlab to not use vectorisation, but for this interpretation the name Array Valued is somewhat misleading.

Simulink: Syntax error

I have a syntax error in the following equation:
((Cs+Cf)*u(1)/Cf-Cs*u(2)/Cf)*(1/(1+(Cs+Cf)/(Cf*(10^(u0/20)))))*(1-exp(-(pi*f1*(10^9)/(fs*10^6))*(Cf/(Cs+Cf)+1/(10^(u0/20)))))
Uppercase/lowercase checked, they are ok. What could be the problem?
As mentioned in the documentation of Interpreted MATLAB Function,
The Interpreted MATLAB Function block accepts one real or complex
input of type double ...
Here you're using multiple inputs which are Cs, Cf, u0, u, f1 and fs.
How to solve it?
Solution-1: Using Interpreted MATLAB Function block
One way to deal with this problem would be to concatenate all the input matrices into a single matrix and use its indices to represent each value in the equation.
e.g; if you have:
u=[1 5]; u0=5; Cs=1; Cf=1; f1=1; fs=20;
Concatenate them into a single matrix in your workspace. Something like the following would do:
new=[u, u0, Cs, Cf, f1, fs];
%It could be different depending on the dimensions of these
%variables that you actually have
then use the following equation according to the indices of new in the Interpreted MATLAB Function block:
((new(4)+new(5))*u(1)/new(5)-new(4)*u(2)/new(5))*(1/(1+(new(4)+new(5))/(new(5)*(10^(new(3)/20)))))*(1-exp(-(pi*new(6)*(10^9)/(new(7)*10^6))*(new(5)/(new(4)+new(5))+1/(10^(new(3)/20)))))
Solution-2: Using MATLAB Function block
You can also use the MATLAB Function block in which you can use multiple inputs. For your case, write the following code in it:
function y = foo(u,u0,Cs,Cf,f1,fs)
y = ((Cs+Cf)*u(1)/Cf-Cs*u(2)/Cf)*(1/(1+(Cs+Cf)/(Cf*(10^(u0/20)))))* ...
(1-exp(-(pi*f1*(10^9)/(fs*10^6))*(Cf/(Cs+Cf)+1/(10^(u0/20)))));
and connect Constant blocks with its inputs and give the values of the constants equal to the respective variables that you want to use.

How to display parameter values at every iteration while using Genetic algorithm in matlab

I am using GA to solve for 6 parameters using the global optimization toolbox.
Is there a way to display the parameter values at every iteration of GA. I can use display or iter but it doesn't necessarily display the parameter values.
Thank you
You can use plot functions.
You can either use one of the predefined plot functions:
options = gaoptimset('PlotFcns',#gaplotbestf);
x= ga(#f,6,[],[],[],[],[],[],[],options)
or you can write your own. For example:
function gademo
options= gaoptimset('PlotFcns',#myplot,'PopulationSize',10);
x= ga(#f,6,[],[],[],[],[],[],[],options)
function y= f(x) % the fitness function
y= norm(x);
end
% simple plot function
function state= myplot(options,state,flag)
fprintf('generation number: %d\n',state.Generation);
fprintf('population:\n');
disp(state.Population);
end
end
Have you tried using the function fprintf?
For example, if you wanted to print the first element of vector x, which is a float, to the screen:
fprintf(1,'%f\n', x(1))
You can see how to format numbers and strings in the documentation of fprintf if you scroll down a bit under formatSpec.

Differentiation with respect to a self-defined function in matlab

I have made a function in func.m which is defined as follows:
function f = func(b)
f = b^2+1;
end
now i wish to differentiate with respect to that function i.e.
find 2*b+1 with a given value for b (lets say 1)
I tried diff(func(1)) but it returned an empty matrix.
Any ideas?
thanks
Your input argument func(1) is a vector of size 1x1. The function diff caled on vectors is this one: here. If you want to use the symbolic one, which allows to differentiate a function but requires the symbolic toolbox, you have to use a symbolic input. Explained here
Example:
% create a symbolic variable x
syms x
%differentiate f(x) and name it f1(x)
f1(x)=diff(f(x))
%get the value at point 1
f1(1)
If you don't have the symbolic toolbox, there is also a numeric solution.

Convert function in Matlab into appropriate form

I have written a function feval that takes two arguments and spits out a number.
Now I wanted to use the command integral2 in order to integrate over my function feval(x,y).
The problem seems to be that integral2 thinks that I have a function that can take two arrays as arguments and apply pairwise operations on them. Unfortunately, this is not the case. My function can only works with 2 numbers and not with full arrays. Is there any standard method to make this work?
Actually, this is my code now and MATLAB claims that
q = integral2( #(x,y) arrayfun(func_cross_scat,x,y),0,2*pi,0,pi);
my function(feval, that i renamed func_cross_scat does not get enough input arguments)
Feed integral2 not with feval, but with feval_wrapper defined as
feval_wrapper = #(x,y) arrayfun(feval, x, y)
x and y can now be arrays (of the same size). This works because arrayfun calls feval for each pair of elements of the input arrays x, y and gives an array as the result.
As a side comment, "feval" is probably not a good name for your function, because Matlab has a built-in feval.