I am using the matlab 2007b and getting in a problem i want to make a function that takes input argument from the user and return sine hperbolic of that value.Sine hyperbolic should be define by that formula sinh(x)=(e^x-e^-x)/2
I am using that code while getting error
function sinh=sinhx(x)
% take the input value of x
a=exp(x)
% assinge exp(x) to a
b=exp(-1*x)
%assinge exp(-x) to variable b
c=a-b
%getting difference of these two variables
d=c/2
% dividing by 2 to get sinhx
end
kindly guide me how can I make this function or correct this code..thanks in advance for your assistance
The simpler the better: you should use the built-in sinh function.
Otherwise, in your function you don't define the output variable sinh, hence the error.
Best
Related
I have this signal:
x(t) = t*sin(m*pi*t)*heaviside(-m*pi-t)+t*cos(k*pi*t)*heaviside(t-k*pi)+sin(k*pi*t)*cos(m*pi*t)*(heaviside(t+m*pi)-heaviside(t-k*pi));
and I want to calculate the values only from -5pi to 5pi with a step of pi/100 using Matlab. How could I do it?
Provided you have defined m and k somewhere, and you have the matlab symbolic toolbox which provides the heaviside function, this is how it is done:
% First we define the function
x = #(t) t*sin(m*pi*t)*heaviside(-m*pi-t)+t*cos(k*pi*t)*heaviside(t-k*pi)+sin(k*pi*t)*cos(m*pi*t)*(heaviside(t+m*pi)-heaviside(t-k*pi));
% Then we define the values for which we want to compute the function
t_values = -5*pi:pi/100:5*pi;
% Finally we evaluate the function
x_values = x(t_values)
Details
First line we define your function as an anonymous function which is a handy tool in matlab.
Then we create a vector of values from -5pi to 5*pi with steps of pi/100. For this we use the matlab colon syntax. It makes it short and efficient.
Finally we evaluate the function on each of the t_values by passing the vector to the anonymous function.
Note: If you don't have the symbolic toolbox, you could easily implement heaviside yourself.
I have to solve a second-degree differential equation and I specifically need the value of the first derivative of y at the final time point. My code is the following:
[T Y]=ode45(#(t y)vdp4(t,y,0.3),[0 1],[0.3/4,((3*0.3)^0.5)/2]);
I know the output will contain the values at which ode45 evaluated the function. To get the y values at specific time value at have it has been advised to give more than two time points in the MATLAB documentation. I did that too.
tspan=[0:0.01:1]
[T Y]=ode45(#(t y)vdp4(t,y,0.3),tspan,[0.3/4,((3*0.3)^0.5)/2]);
The T vector still does not have all the values from 0 to 1 (The last value is 0.39). This happens especially after multiple executions of ode45 function. I found something else in the MATLAB documentation: using "sol" structure to deval the values for specific t values. Is that the right way to go?
For reference, my differential equation is in the following function.
function dy = vdp4(t,y,k)
dy = zeros(2,1); % a column vector
dy(1)=y(2);
dy(2)=(y(2)^2-2*t*y(2)+2*y(1))/k+2;
end
Edit: I provided the parameter value. It should now be executable.
Try to plot your solution, you will find the answer
Is it possible to use the optimization function fmincon with a Matlab defined function?
I wrote a function where I give few constant parameters (real or complex) and for now, every time I change these parameters, the result changes (you don't say).
[output1, output2] = my_function(input1,input2,input3,input4)
I saw that fmincon function allows to find the optimum result with a given constraint. Let's say that I want to find the optimum output acting only on input1 and keeping constant all the others inputs. Is it possible to define something like
fmincon(#(input1)my_function,[1,2],[],mean)
for input1 that goes from 1 to 2 for the best value mean, where mean is the mean value of some other results.
I know that is a quite vague question but I'm not able to give a minimum example, since function makes a lot of things
The first attept with multiple outputs gave me the error Only functions can return multiple values.
Then I tried with only one output and if I use
output1 = #(input1)function(input2,input3);
fmincon(#output1,[1,2],[],mean)
I get the error
Error: "output1" was previously used as a variable, conflicting with its use here as the name of a function or command.
See "How MATLAB Recognizes Command Syntax" in the MATLAB documentation for details.
With fmincon(#my_function,[1,2],[],mean) I get Not enough input arguments.
The input should be used in your function definition - read up on how anonymous functions should be written. You don't have to use anonymous functions to define the actual objective function (myFunction below), you can use functions in their own file. The key is that the objective function should return a scalar to be minimised.
Here is a very simple example, using fmincon to find the minima in myFunction, based on the initial guess [1.5,1.5].
% myFunction is min when x=1,y=2
myFunction = #(x,y) (x-1).^2 + (y-2).^2;
% Define the optimisation function.
% This should take one input (can be an array)
% and output a scalar to be minimised
optimFunc = #(P) myFunction( P(1), P(2) );
% Use fmincon to find the optimum solution, based on some initial guess
optimSoln = fmincon( optimFunc, [1.5, 1.5] );
% >> optimSoln
% optimSoln =
% 0.999999990065893 1.999999988824129
% Optimal x = optimSoln(1), optimal y = optimSoln(2);
You can see the calculated optimum isn't exactly [1,2], but it's within the default optimality tolerance. You can change the options for the fmincon solver - read the documentation.
If you wanted to keep y=1 as a constant, you just need to update the function definition:
% We only want solutions with y=1
optimFunc_y1 = #(P) myFunction( P(1), 1 ); % y=1 always
% Find new optimal solution
optimSoln_y1 = fmincon( optimFunc_y1, 1.5 );
% >> optimSoln_y1
% optimSoln_y1 =
% 0.999999990065893
% Optimal x when y=1 = optimSoln(1)
You can add inequality constraints using the A, B, Aeq and Beq inputs to fmincon, but that's too broad to go into here, please refer to the docs.
Note that you're using the keyword function in a way which is invalid syntax. I've instead used valid variable names for the functions in my demo.
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.
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.