How may I use cellfun or arrayfun in place of this loop? - matlab

I implemented the following function in Matlab.
function [x y] = cloud(a,b,phi,x0,y0,N)
phi=phi*2*pi/360;
m=ceil(5*N/pi);
x=a*(-1+2*rand(m,1));
y=b*(-1+2*rand(m,1));
f=sqrt(a^2 - b^2);
indexMatrix=zeros(m,3);
indexMatrix(:,1)=x;
indexMatrix(:,2)=y;
insidePointsMatrix=zeros(1,2);
j=1;
for i=1:m
insidePoint=sqrt(((x(i)+f).^2) + (y(i))^2) + sqrt(((x(i)-f).^2) + (y(i))^2);
if (insidePoint<=2*a)
indexMatrix(i,3)=1;
%insidePointsMatrix(j,:)=indexMatrix(i,1:2);
if j<=N
insidePointsMatrix(j,1:2)=indexMatrix(i,1:2);
j=j+1;
end
end
end
I am reading about arrayfun and cellfun, and I am wondering if it is possible to shorten the function I already implemented using it. Given a 2 x n matrix A where (u,v) are the entries for any given row, how can I return a matrix listing the output of the following formula for each row?
sqrt((u+f).^2+v.^2) + sqrt((u-v)^2+v^2)
The variables f and a are defined above. I am trying to transform a series of points into an ellipse.

using find should give you the u,v coordinates:
cond=sqrt(((x+f).^2) + y.^2) + sqrt(((x-f).^2) + y.^2) ;
ind=find(cond<=2*a);
u=x(ind);
v=y(ind);

Related

how to call and use function Recursivity in matlab

I have an exercise with newton way calculate y(x+1)=y(x)-f(x)/f'(x) in this function I need y(x) and for this I use function Recursivity for y(1) & y(2) it's working because y(1) has formula y(1)=R*T/p ,for save y(x) I use zeros()to use when calculate y(x+1) but for x>2 I get the same answer,what am I missing??what can I use instead of zeros() for save and access to newt(x-1)
function y= newt(x)
%define beta,gamma,delta,....there
y(1)=R*T/p;
answ=zeros(1,20);
z=0;
if x==1
f=(R*T*y(1)^3)+(beta*y(1)^2)+(gamma*y(1))+delta-(p*y(1)^4);
f1=(3*y(1)^2*R*T)+(2*y(1)*beta)+gamma+(4*p*y(1)^3);
answ(1) = y(1);
fprintf('n=1 v=%f\n',y(1));
else
y=newt(x-1);
f=(R*T*y^3)+(beta*y^2)+(gamma*y)+delta-(p*y^4);
f1=(3*y^2*R*T)+(2*y*delta)+gamma+(4*p*y^3);
z=y-f/f1
answ(1,2:x)=z;
end
answ(1) = y(1);
answ(1,2:x)=z;
according to #patrik guidance,I find my answer
f=#(v)p*v^4-R*T*v^3-beta*v^2-gamma*v-delta;
f1=#(v)4*p*v^3-3*R*T*v^2-2*beta*v-gamma;

matlab optimization: objective function with dependent decision variable

I want to optimize an objective function with dependent decision variables as below.
Sum [I * (x(i) - x(i-1) + lo(i) - g(i)) * p(i)]
Please note that the decision variable is only x(i) and x(i-1) is a value came from previous step of optimization.
I have no idea how to write this objective function. Should I use function handler? Thanks
Perhaps this is what you're asking?
Imagine you have a 3 by 1 vector x.
[x_1
x = x_2
x_3]
and you want to compute:
[x_1 [0
y= x_2 - x_1
x_3 x_2]
You could do this in Matlab with the code:
y = x - [0;x(1:end-1)];
this works since x(1:end-1) will refer to [x_1; x_2]. You can use this snippet to write your overall objective function.

Get function handle of fit function in matlab and assign fit parameters

I'm fitting custom functions to my data.
After obtaining the fit I would like to get something like a function handle of my fit function including the parameters set to the ones found by the fit.
I know I can get the model with
formula(fit)
and I can get the parameters with
coeffvalues(fit)
but is there any easy way to combine the two in one step?
This little loop will do the trick:
x = (1:100).'; %'
y = 1*x.^5 + 2*x.^4 + 3*x.^3 + 4*x.^2 + 5*x + 6;
fitobject = fit(x,y,'poly5');
cvalues = coeffvalues(fitobject);
cnames = coeffnames(fitobject);
output = formula(fitobject);
for ii=1:1:numel(cvalues)
cname = cnames{ii};
cvalue = num2str(cvalues(ii));
output = strrep(output, cname , cvalue);
end
output = 1*x^5 + 2*x^4 + 3*x^3 + 4*x^2 + 5*x + 6
The loop needs to be adapted to the number of coefficients of your fit.
Edit: two slight changes in order to fully answer the question.
fhandle = #(x) eval(output)
returns a function handle. Secondly output as given by your procedure doesn't work, as the power operation reads .^ instead of x, which can obviously be replaced by
strrep(output, '^', '.^');
You can use the Matlab curve fitting function, polyfit.
p = polyfit(x,y,n)
So, p contains the coefficients of the polynomial, x and y are the coordinates of the function you're trying to fit. n is the order of the polynomial. For example, n=1 is linear, n=2 is quadratic, etc. For more info, see this documentation centre link. The only issue is that you may not want a polynomial fit, in which case you'll have to use different method.
Oh, and you can use the calculated coefficients p to to re-evaluate the polynomial with:
f = polyval(p,x);
Here, f is the value of the polynomial with coefficients p evaluated at points x.

Error using ==> plot Vectors must be the same lengths

So I have the following two functions:
delta_t=T/N_time;
delta_x=1/N_space;
rho=delta_t/delta_x^2;
phi=zeros(N_space+1,N_time+1);
phi(:,1)=initial_condition((0:N_space)*delta_x);
for j=1:N_time;
for i=2:N_space;
phi(i,j+1)=rho*(phi(i-1,j)+phi(i+1,j))+(1-2*rho)*phi(i,j);
end;
end;
phi = phi(end,:);
end
When I run my second function, I get this error:
Error using ==> plot Vectors must be the same lengths.
I don't know why the sizes are different. They should be the same since the second function calls the first function. Any help would be much appreciated.
I think you have your phi transposed somehow. Your x_var is an 1 x (N_space + 1) vector, your phi is (N_space + 1) x (N_time + 1) and you return phi(end, :), which will be 1 x (N_time + 1). Did you mean to return phi(:, end)?

Linear combination of a string vector (w/functions) and number vector (coefficients)

I'm really new at matlab, and am trying to fit a line or curve to data points for homework (that part is actually done). Now, I want to take this a little further than the homework asked, I have constructed a function that takes in a text file with coordinates and any number of functions (1,x,x^2... e.g.) and determines the coefficients.
So in the end I'm left with two vectors: one with the coefficients: C = [a,b,c] and another one with functions: F = {'1','x','x^2'}, and I'd like to create a linear combination of them: l = a + b*x + c*x^2, to plot the curve on a graph, and for some reason I can't figure out how to get that to work. Is there something obvious I'm overlooking, or do I have to rethink this in some way?
a=1; b=2; c=3;
C=[a,b,c];
CS = cellfun(#num2str,num2cell(C),'uniformoutput',0)
M={'*','*','*'};
F={'1','x','x^2'};
P={' + ',' + ',''};
S=reshape([CS; M; F; P],1,[]);
cat(2,S{:})
Output:
ans =
1*1 + 2*x + 3*x^2
Are you sure you want to print 'a', 'b' and 'c' as chars?