Why does this not correctly evaluate e^x using the Taylor series? - matlab

I am attempting to write a function called expSeries which uses another function factFunc to evaluate e^x. I have already written the function factFunc, as shown below:
function fact = factFunc(n)
f = 1;
for a = 1:b
f = f*a;
end
fact = f;
end
I am now attempting to write the function expSeries which evaulates e^x using the Taylor series. This is what I have so far:
function expo = exponentialFunc(x)
terms = input('Enter the number of terms');
b = 0;
for i = 1:terms
b = x/factFunc(terms);
end
expo = b;
end
And in the main program, I have
n = exponentialFunc(4);
disp(n);
Where in this instance I am trying to find e^4. However, the output is not what expected. Does anyone have any idea where I am going wrong?

Fix to factFunc:
function fact = factFunc(n)
f = 1;
for a = 1:n
f = f*a;
end
fact = f;
end
Fix to exponentialFunc
function expo = exponentialFunc(x)
terms = input('Enter the number of terms');
b = 0;
for i = 0:terms-1
b = b + x^i/factFunc(i);
end
expo = b;
end
Example
>> exponentialFunc(4)
Enter the number of terms10
ans =
54.1541
Note exp(4) = 54.59815...

Related

matlab finite element method (error:Not enough input arguments)

I have an issue in compiling my code in Matlab the error showing in the message is: Not enough input argument
I don't know how to solve it or where is the problem and I follow many documentations in the Mathworks website describes how to generalize problems 1-D with Neumann boundary conditions.
this is my code?
thank you in advance!
function [c,f,s] = oscpde(x,t,u,dudx)
c = 0;
f = dudx;
s = u-x*(x-1);
end
%----------------------------------------------
function u0 = oscic(x)
u0 = ((exp(1)*exp(1)+1))/(1-exp(1))*exp(0) + ((exp(1)*exp(1)+exp(1))/(1-exp(1)))*exp(0)+ 2;
end
%----------------------------------------------
function [pl,ql,pr,qr] = oscbc(xl,ul,xr,ur,t)
pl = 0;
ql = 1;
pr = 0;
qr = 1;
end
%----------------------------------------------
function [value, isterminal, direction] = pdevents(m,t,xmesh,umesh)
value = umesh;
isterminal = zeros(size(umesh));
direction = zeros(size(umesh));
end
**the error is:
Not enough input arguments.
Error in CODE (line 3)
f = dudx;
**

Anonymous function defined by a loop in matlab

I use anonymous functions in my code. For example:
G = #(x) [norm(A*x-b);A'*(A*x-b)];
norm(Ax-b) is the objective function and A'*(Ax-b) the gradient.
Then,
Algo( G,varagin );
etc
What I would like to do is to define f with a loop:
n = 9;
k = 2;
t = 1 - x.^k;
f = 0;
for i=1:n
f = f + x(i,1)*prod(t(1:i-1));
end
grad_f = zeros(n,1);
for i0=1:n
s = t;
s(i0) = [];
for i=i0+1:n
grad_f(i0) = grad_f(i0) + x(i)*prod(s(1:i0-1));
end
grad_f(i0) = -k*x(i0)^(k-1)*grad_f(i0);
grad_f(i0) = grad_f(i0) + prod(t(1:i0-1));
end
Then I would like to do something like:
" G = #(x) [f,grad_f] "
Thanks a lot for your help!
Found the answer:
create F(x) and GRAD_F(x) as functions in matlab computing f and grad_f respectively.
Then:
G = #(x) [F(x);GRAD_F(x)];
Algo(G,varargin);

Matlab. Create a loop to change variable size with each iteration

I am currently trying to run a script that calls a particular function, but want to call the function inside a loop that halfs one of the input variables for roughly 4 iterations.
in the code below the function has been replaced for another for loop and the inputs stated above.
the for loop is running an Euler method on the function, and works fine, its just trying to run it with the repeated smaller step size im having trouble with.
any help is welcomed.
f = '3*exp(-x)-0.4*y';
xa = 0;
xb = 3;
ya = 5;
n = 2;
h=(xb-xa)/n;
x = xa:h:xb;
% h = zeros(1,4);
y = zeros(1,length(x));
F = inline(f);
y(1) = ya;
for j = 1:4
hOld = h;
hNew = hOld*0.5;
hOld = subs(y(1),'h',hNew);
for i = 1:(length(x)-1)
k1 = F(x(i),y(i));
y(i+1,j+1) = y(i) + h*k1;
end
end
disp(h)
after your comment, something like this
for j = 1:4
h=h/2;
x = xa:h:xb;
y = zeros(1,length(x));
y(1) = ya;
for i = 1:(length(x)-1)
k1 = F(x(i),y(i));
y(i+1,j+1) = y(i) + h*k1;
end
end

Handle function implicitly accounting for independent variables

I have that
clc, clear all, close all
tic
k1 = 1E-02:0.1:1E+02;
k2 = 1E-02:0.1:1E+02;
k3 = 1E-02:0.1:1E+02;
k = sqrt(k1.^2+k2.^2+k3.^2);
c = 1.476;
gamma = 3.9;
colors = {'b'};
Ek = (1.453*k.^4)./((1 + k.^2).^(17/6));
E = #(k) (1.453*k.^4)./((1 + k.^2).^(17/6));
E_int = zeros(1,numel(k));
E_int(1) = 1.5;
for i = 2:numel(k)
if k(i) < 400
E_int(i) = E_int(i-1) - integral(E,k(i-1),k(i));
elseif k(i) > 400
E_int(i) = 2.180/(k(i)^(2/3));
end %end if
end %end i
beta = (c*gamma)./(k.*sqrt(E_int));
figure
plot(k,beta,colors{1})
count = 0;
%F_11 = zeros(1,numel(k1));
F_33 = zeros(1,numel(k1));
Afterwards, I should calculate F_33 as
for i = 1:numel(k1)
count = count + 1;
phi_33 = #(k2,k3) (1.453./(4.*pi)).*(((k1(i)^2+k2.^2+(k3 + beta(i).*k1(i)).^2).^2)./((k1(i)^2+k2.^2+k3.^2).^2)).*((k1(i)^2+k2.^2)./((1+k1(i)^2+k2.^2+(k3+beta(i).*k1(i)).^2).^(17/6)));
F_33(count) = 4*integral2(phi_33,0,1000,0,1000);
end
Now let's come to my question. I know from a paper that:
k = sqrt(k1.^2+k2.^2+k3.^2);
k30 = k3 + beta.*k1;
k0 = sqrt(k1.^2+k2.^2+k30.^2);
E_k0 = 1.453.*(k0.^4./((1+k0.^2).^(17/6)));
Therefore the expression for phi_33 would result in
phi_33 = (E_k0./(4*pi.*(k.^4))).*(k1.^2+k2.^2);
The question is: how can I make use of this final expression insted of the long one I'm using at the moment (within the for loop)?
The last expression for phi_33 is easier to handle (especially because of reckless mistakes in writing the former) and it would "pass by reference" (k2,k3), which are the independent variables.
Any hint is more than welcome.
Best regards,
fpe
If I understand you correctly you want to use the new expression in exactly the same way as the old one-liner. You just want to divide your function phi33 into parts because of readability.
You could do this by placing the expression in a separate function taking all values needed for the calculation. Using your old expression exactly this would look something like this:
function phi_33 = phi_33_old(k1,k2,k3,beta,i)
phi_33 = (1.453./(4.*pi)).*(((k1(i)^2+k2.^2+(k3 + beta(i).*k1(i)).^2).^2)./((k1(i)^2+k2.^2+k3.^2).^2)).*((k1(i)^2+k2.^2)./((1+k1(i)^2+k2.^2+(k3+beta(i).*k1(i)).^2).^(17/6)));
end
This function could then be called inside your for-loop like this.
phi_33_test = #(k2,k3) phi_33_old(k1,k2,k3,beta,i);
Using the same style, a new function could be defined as follows.
function phi_33 = phi_33_new(k1,k2,k3,beta,i)
k = sqrt(k1.^2+k2.^2+k3.^2);
k30 = k3 + beta.*k1;
k0 = sqrt(k1.^2+k2.^2+k30.^2);
E_k0 = 1.453.*(k0.^4./((1+k0.^2).^(17/6)));
phi_33_allValues = (E_k0./(4*pi.*(k.^4))).*(k1.^2+k2.^2);
phi_33 = phi_33_allValues(i);
end
Note that here all values of phi_33 are calculated and then the ith value is selected. It is written in this way only to show the similarity to the old case. This new function can now be called inside the for-loop in the same way as the old one.
phi_33 = #(k2,k3) phi_33_new(k1,k2,k3,beta,i);

How to call a function in a for loop in MATLAB?

I would like to call (execute) an m-file (function) in a loop like this:
global m, r
m = 2;
for n = 2:10;
for r1 = 0:n-m;
r2 = n-m-r1;
r = [r1,r2];
[Call the function here?????????]
end
end
This is the function:
function main
x0 = [-0.5403,0.5471];
fsolve(#fcn,x0)
function z = fcn(X)
rand('twister',5409);
global m, r
a = rand(m,1);
for i = 1:m
sm(i) = 0.0;
for l = m-i+1:m
sm(i) = sm(i)+r(l);
end
s = 1.0/(i+sm(i));
g(i) = (a(i))^s;
end
prod = 1.0;
for k = 1:m
prod = prod * g(m+1-k);
u(k) = 1.0-prod;
x(k) = (sqrt(3)/pi)*log(u(k)/(1-u(k)));
end
sum = 0;
sum1 = 0;
sum2 = 0;
for j = 1:m
sum = sum+(r(j)+2)*(1/(1+exp((-pi/sqrt(3))*((x(j)-X(1))/X(2)))));
sum1 = sum1+(r(j)+2)*((x(j)-X(1))/X(2))*(1/(1+exp((-pi/sqrt(3))*((x(j)-X(1))/X(2)))));
sum2 = sum2+(x(j)-X(1))/X(2);
end
z(1) = pi/(X(2)*sqrt(3))*(-m+sum);
z(2) =(-1/X(2))*(m+(pi/sqrt(3))*(sum2-sum1));
Thank you very much for your help.
The functions main and fcn should be saved in a file called "main.m". You have to make sure this file is either in your current working directory or somewhere on the MATLAB path (as mentioned in a comment by Amro) so that MATLAB can get to it. Since main requires no input arguments and has no output arguments, you could then just call it in any one of the following ways:
main
main;
main()
main();
If you have a function Main.m
Main.m
function out = main(in)
% blah blah blah
You would call the function
in = 2;
out = main(in)
Does this make sense?
Personnally I would create your function without a main() part.
create a file called
fcn.m
with your function fcn in it, make sure it's in your working directory or in your matlab path and then call it inside your loop.
addpath(genpath('/the/path/to/your/function/');
global m, r
m = 2;
for n = 2:10;
for r1 = 0:n-m;
r2 = n-m-r1;
r = [r1,r2];
z=fcn(r)
end
end