I want to convert this equation from Matlab to TCL, Can anyone help.
t = 2*pi*rand(n,1);
r = R*sqrt(rand(n,1));
x = x0 + r.*cos(t);
y = y0 + r.*sin(t);
Though there are many similarities, MATLAB has one key difference from Tcl: as well as single values, it can work on vectors, matrices and tensors. This makes translating your code a little more involved. (Apart from that, = maps to set, and expressions are computed with expr.)
# Example initial parameters
set n 123
set R 321
set x0 456
set y0 654
# The translation of your code
set x {}
set y {}
set pi 3.1415927
for {set i 0} {$i < $n} {incr i} {
set t [expr {2 * $pi * rand()}]
set r [expr {$R * rand()}]
lappend x [expr {$x0 + $r * cos($t)}]
lappend y [expr {$y0 + $r * sin($t)}]
}
Note that shorter forms than this are possible through the use of an extension package such as VecTcl.
Related
For example, FX = x ^ 2 + sin (x)
Just for curiosity, I don't want to use the CVX toolbox to do this.
You can check this within some interval [a,b] by checking if the second derivative is nonnegative. For this you have to define a vector of x-values, find the numerical second derivative and check whether it is not too negative:
a = 0;
b = 1;
margin = 1e-5;
point_count = 100;
f=#(x) x.^2 + sin(x);
x = linspace(a, b, point_count)
is_convex = all(diff(x, 2) > -margin);
Since this is a numerical test, you need to adjust the parameter to the properties of the function, that is if the function does wild things on a small scale we might not be able to pick it up. E.g. with the parameters above the test will falsely report the function f=#(x)sin(99.5*2*pi*x-3) as convex.
clear
syms x real
syms f(x) d(x) d1(x)
f = x^2 + sin(x)
d = diff(f,x,2)==0
d1 = diff(f,x,2)
expSolution = solve(d, x)
if size(expSolution,1) == 0
if eval(subs(d1,x,0))>0
disp("condition 1- the graph is concave upward");
else
disp("condition 2 - the graph is concave download");
end
else
disp("condition 3 -- not certain")
end
I would like to achieve the above for the following:
Rn = 0.009; % Resolution of simulation (in m^3)
Xs = -1 : Rn : 1;
Ys = -1 : Rn : 1;
Zs = 0 : Rn : 1;
[X Y Z] = meshgrid(Xs, Ys, Zs);
alpha = atan2(Z,X);
ze = x.^2 + y.^2; % define some condition
m = 0.59; % manual input
cond = (pi/3 <= alpha) & ...
(alpha <= (2*pi/3)) & ...
(m <= Z) & ...
(Z <= ze); % more conditions
xl = nnz(cond); % the number of non-zero elements
f = abs(xl*1000 - 90) % guessing m to get f as low as possible
How do I turn m into a variable for some f function so I can call fminsearch to quickly find the corresponding m for f ≈ 0?
In order to use m as a variable, you need to define a function handle. So you need to write:
cond = #(m) ((pi/3) <= alpha) & (alpha <= (2*pi/3)) & (m <= Z) & (Z <= ze);
However, you cannot use a function handle in the nnz routine, since it only accepts matrices as inputs. But, the solution to the problem is that you only have Boolean variables in cond. This means, you can simply sum over cond and get the same result as with nnz.
The only issue I see is how to implement the sum in fminsearch. Unfortunately, I do not have access to fminsearch, however I would assume that you can do something with reshape and then multiply with dot (i.e. .*) with the unity vector to get a sum. But you'll have to try that one out, not sure about it.
This question is connected to this one. Suppose again the following code:
syms x
f = 1/(x^2+4*x+9)
Now taylor allows the function f to be expanded about infinity:
ts = taylor(f,x,inf,'Order',100)
But the following code
c = coeffs(ts)
produces errors, because the series does not contain positive powers of x (it contains negative powers of x).
In such a case, what code should be used?
Since the Taylor Expansion around infinity was likely performed with the substitution y = 1/x and expanded around 0, I would explicitly make that substitution to make the power positive for use on coeffs:
syms x y
f = 1/(x^2+4x+9);
ts = taylor(f,x,inf,'Order',100);
[c,ty] = coeffs(subs(ts,x,1/y),y);
tx = subs(ty,y,1/x);
The output from taylor is not a multivariate polynomial, so coeffs won't work in this case. One thing you can try is using collect (you may get the same or similar result from using simplify):
syms x
f = 1/(x^2 + 4*x + 9);
ts = series(f,x,Inf,'Order',5) % 4-th order Puiseux series of f about 0
c = collect(ts)
which returns
ts =
1/x^2 - 4/x^3 + 7/x^4 + 8/x^5 - 95/x^6
c =
(x^4 - 4*x^3 + 7*x^2 + 8*x - 95)/x^6
Then you can use numden to extract the numerator and denominator from either c or ts:
[n,d] = numden(ts)
which returns the following polynomials:
n =
x^4 - 4*x^3 + 7*x^2 + 8*x - 95
d =
x^6
coeffs can then be used on the numerator. You may find other functions listed here helpful as well.
I'm very new to Matlab and have problem plotting this nonlinear 2D function graph using Matlab.
a lot of errors generated after the below is run.
fun1 = 20 + 10 + 15;
fun2 = 20 + (x * 0.00125 ) + 15;
fun3 = (x * 0.0025) + 15;
fplot(fun1,[0 8000])
fplot(fun2,[8000 16000])
fplot(fun2,[16000 positive infinity])
I appreciate a lot to your efforts and kindness for replying my question
Best Regards
Your first three expressions do not define functions. Please read the documentation about the correct syntax.
fun1 = #(x)(20 + 10 + 15);
First create a file fun.m which contains your function definition
function y = fun(x)
if x < 8000
y = 20 + 10 + 15;
elseif x < 16000
y = 20 + (x * 0.00125) + 15;
else
y = x * 0.0025 + 15;
end
end
Then you can plot it with
fplot(#fun, [0 24000])
which results in
If you do some reading in fplot you will find out that
for fplot(fun,limits)
fun must be
The name of a function
A string with variable x that may be passed to eval, such as 'sin(x)', 'diric(x,10)', or '[sin(x),cos(x)]'
A function handle
so in your case you need to change all of you fun to strings just add ' before and after the expression
as for the last line change it to be
fplot(fun2,[16000 inf])
although i don't think this would give you any good results
I want to implement Discrete Integration with Galois Fields in Matlab where the time step is not constant.
Assume that it is this:
My attempt
function [ int ] = integrate_matlab( YDataVector, a, b )
%integrate_matlab Calculate the discrete integral
% Discrete Matlab Integration
% int_1^N x(t_k) * (b-a)/N, where t_k = a + (b-a) k/N
%
% YDataVector - Galois vector (255 x 1 gf), this is signal,
% which values you can reach by YDataVector.x
%
% int - returns Galois vector (255 x 1 gf)
N = length(YDataVector);
for k=1:N
tk = a + (b - a) * k/N;
int = xtk(YDataVector, k) * (b - a) / N;
% How to implement the function xtk(YDataVector)?
end
and then the function xtk
function [ xtk_result ] = xtk( YDataVector, k )
%xkt Summary of this function goes here
% YDataVector - Galois vector (255 x 1 gf), this is signal
% xtk_result - Galois vector (255 x 1 gf)
% k - index, this must be here to be able calculate different xtk for different iterations
xtk_result = ; // I do not know what to fill here
end
I am confused by the mathematical series equation x(tk) for tk.
I know that I am doing now this wrong.
The writing x(tk) just confuses me, since I think it as a function that takes in the series.
I know that it is a signal at some time point, here the YDataVector, but how to implement it I have forgotten.
I should probably iterate the series first:
t_0 = a;
t_1 = a + (b - a) * 1/N;
This does not seem to help, since tk is not defined iteratively.
What am I thinking wrong when implementing the series x(tk)?
Assuming that t contains the time that corresponds to each element of x (stored in YDataVector.x). Then if I understood correctly your question you can get x_tk with something like this :
N = length(YDataVector.x) ;
k = 1 : N;
tk = a + (b-a)* k/N ;
x_tk = interp1(t,YDataVector.x,tk);