Simulink: Syntax error - matlab

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.

Related

Display code in Command Window like 'help' in Matlab

I am looking for a way to display the code of a function (unformatted) in the command window. In this way I want to easily check what the expected input arguments are, the help text and perhaps some part of the code.
When writing
help functionname
I only get the help text
Is there a way to get the complete code?
Solution for stantard (non built-in) functions
What you want can be done as
type functionname
Example:
>> type mean
function y = mean(x,dim,flag,flag2)
%MEAN Average or mean value.
% S = MEAN(X) is the mean value of the elements in X if X is a vector.
% For matrices, S is a row vector containing the mean value of each
% column.
···
Work-around for built-in functions
Built-in functions don't have Matlab code; they are directly part of the interpreter. For those functions the above method doesn't work. For example:
>> type find
'find' is a built-in function.
However, usually there is still a function file, which consists of comments only. You can open it with
open find
This will open the file find.m in the Matlab editor, which contains:
%FIND Find indices of nonzero elements.
% I = FIND(X) returns the linear indices corresponding to
% the nonzero entries of the array X. X may be a logical expression.
% Use IND2SUB(SIZE(X),I) to calculate multiple subscripts from
···

how to create a function to calculate hyperbolic sine of values

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

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.

matlab genetic algorithm solver complex input and output

My Matlab program has multiple inputs as a struct (in.a, in.b, etc.)
and multiple outputs (out.a, out.b, etc.)
I would like to use the genetic algorithm solver from teh optimization toolbox to find the best input in.a, while all the other inputs are constant. The fitness is one of the outputs, e.g. out.b(2,3).
How do I "tell" the solver this?
Thanks
Daniel
It is not uncommon in programming to have a situation where what is most convenient for your function and what some library call expects of it don't agree. The normal resolution to such a problem is to write a small layer in between that allows the two to talk; an interface.
From help ga:
X = GA(FITNESSFCN,NVARS) finds a local unconstrained minimum X to the
FITNESSFCN using GA. [...] FITNESSFCN accepts a vector X of size
1-by-NVARS, and returns a scalar evaluated at X.
So, ga expects vector input, scalar output, whereas you have a structure going in and out. You would have to write the following (sub)function:
function Y = wrapper_Objfun(X, in)
in.a = X; %# variable being optimized
out = YOUR_REAL_FUNCTION(in); %# call to your actual function
Y = out.b(2,3); %# objective value
end
and then the call to ga will look like
X = ga(#(x) wrapper_Objfun(x,in), N);
where N is however large in.a should be.
Also have a read about it in Matlab's own documentation on the subject.

Matrix dimension error while calling mldivide in MATLAB

I am getting this error while running my code:
Error using ==> mldivide Matrix dimensions must agree.
Here is my code :
%make the plots of phase and group velocity vs discreteness of the grid
c=1;
a=input('Please enter the ratio cdt/dx : ')
figure(1)
R=2:40;
plot(R,phase_vel(R,a)/c)
xlabel('R=l/dx')
ylabel('u_phase/c')
%figure(2)
%plot(group_vel(R,a),R,0,40)
%xlabel('R=l/dx')
%ylabel('u_group/c')
and here are my functions :
function phase_velocity = phase_vel(R,a)
%numerical phase velocity of the discrete wave
c=1;
phase_velocity=(2*pi*c)/(R*knum(R,a));
end
function group_velocity =group_vel(R,a )
%numerical group velocity of the discrete wave
c=1;
group_velocity=(a*sin(knum(R,a)))/(sin(2*pi*a/R))
end
function knumber = knum(R,a)
%This is the k wave number
knumber=acos((1/a)^2*(cos(2*pi*a/R)-1)+1);
end
How can I resolve this error?
EDIT: I used . operator in every equation and i changed the limits of R=4:40
If your goal is to apply your formulas to each individual value in the vector R then you should be performing all of your computations using the element-wise arithmetic operators .*, ./, and .^ instead of the matrix operators *, /, and ^.
Your error is probably occurring in the first call to your function knum, specifically when you try to compute 2*pi*a/R. Since 2*pi*a is a single scalar value, you get an error when trying to perform matrix right division / using the row vector R. The really weird thing is the error message:
??? Error using ==> mldivide
Matrix dimensions must agree.
which implies you are using the matrix left division operator \, which you clearly aren't. I tested this in MATLAB R2010b and I get the same incorrect function name appearing in my message. I think this may just be a typo in the error message, and I've dropped a note to the MATLAB folks to take a look at it and clear it up.
I don't have the Symbolic Math Toolbox, but your problem seems to be that you are using plot, a function which can deal with arrays of numbers, and feeding it the result of a symbolic calculation. Have a look at the Matlab Help, where the Topic Creating Plots of Symbolic Functions suggests using ezplot(). Alternatively you need to evaluate your symbolic expression for certain input values to create an array of numbers that plot can deal with - but you can't use double() for that since it wouldn't know what numbers to plug into your variables.