I have been exploring the Matlab symbolic toolbox, in preparation for an upcoming final. However, I seem not to be able to convert a string inputted from the user to a symbolic expression, and then use it for integration.
a = input('Plese enter a = ');
b = input('Please enter b = ');
function1 = input('Please enter the function: ', 's');
syms x eq1
eq1 = sym(function1);
Integral1 = int(eq1 , x, a, b);
Simpson = 1 / 6 * (b - a) * (subs(eq1 , x, a) + 4 * ...
(subs(eq1 , x, (a+b)/2))...
+ subs(eq1 , x, b));
fprintf('The value of the integral is %s \n', Integral1);
fprtinf('The aprroximation with simp is %s \n', Simpson);
Simpson is the integral approximation from simpson's rule. The error I get is something along the lines of "Conversion to 'sym' returned the MuPAD error: Error: unexpected 'identifier'" the line number would be the line
eq1 = sym(function1);
One way to do this is using eval:
function1 = input('Please enter the function: ', 's');
eq1=eval(function1);
But you need to convert the results to doubles before you can display them with fprintf:
fprintf(''The value of the integral is %s \n', double(Integral1));
Starting R2017b, use str2sym
>> syms f(x)
>> function1 = input('Please enter the function: ', 's');
Please enter the function: sin(x)
>> f(x) = str2sym(function1)
f(x) =
sin(x)
See: https://www.mathworks.com/help/symbolic/str2sym.html
Related
I want to use a equation which is stored in variable using inline Function but I am getting an error of 'Check for incorrect argument data type or missing argument in call to function 'coeffs'.
'. Here is the code and the output I want. Matlab
syms x;
eq = input('Enter Equation Of Divisor: ','s');
p = inline(eq);
c = coeffs(p);
disp(c);
> Enter Equation: x-4
> [-4,1]
Thanks.
Use the following code:
syms x
eq = input('Enter Equation Of Divisor: ');
c = coeffs(eq);
disp(c)
I want to integrate x^2 from 2 to 4 with the trapezoidal integration method. For this, I defined a function trap that takes 4 arguments:
function y = trap( fn, a, b, h )
n = (b-a)/h;
x = a + [1:n-1]*h;
y = h/2*(feval(fn, a) + feval(fn, b) + 2*sum(feval(fn,x)));
and a function f
function y= f(x)
y=x^2
end
Now, by executing trap(f,2,4,0.1), I get the following error:
Not enough input arguments.
Error in f (line 2)
y=x^2
What is the origin of that error?
You have to call trap using the function handle #f, not f.
trap(#f,2,4,0.1)
function y = trap( fn, a, b, h )
n = (b-a)/h;
x = a + [1:n-1]*h;
y = h/2*(fn(a) + fn(b) + 2*sum(fn(x)));
end
function y= f(x)
y = x.^2;
end
which gives, as expected,
ans =
18.67
Also you needed element-wise multiplication in f(x) to compute y = x.^2.
And feval is not necessary. You can directly call fn(a) to evaluate the function.
I am trying to compute the taylor series of ln(x) for any value of x.
What I have so far is:
clear
clc
n = input('Enter number of iiterations (n): ' );
x = input('enter value of x (x): ');
y = zeros(1,n);
for i = 0:n
y(i+1)=sum + (-1)^(n+1)*(x-1)^n/n;
end
But this code seems to be broken and I can't figure out why. Any suggestions on how to improve?
This is a one liner in addition to the for-loop answer provided by #farbiondriven
For 0<x<1 :
sumLn = #(x, n)(sum(((-1).^(0:n-1)).*((x-1).^(1:n))./(1:n)));
sumLn(0.5,10)
ans =
-0.6931
>> log(0.5)
ans =
-0.6931
For x>0.5 :
sumLn = #(x, n)(sum( ((x-1)/x).^(1:n) ./ (1:n) ));
sumLn(2,10)
ans =
0.6931
log(2) =
0.6931
Note: The variable x in this formula is bounded as mentioned in this link.
Try this:
clear
clc
n = input('Enter number of iterations (n): ' );
x = input('enter value of x with abs value < 1 (x): ');
y = zeros(1,n+1);
y(1)=0;
for i = 1:n
y(i+1)= y(i) + ((-1)^(i+1)*(x-1)^i/i);
end
txt = sprintf('The output is: %f', y(n+1))
I suggest using built-in function and hopefully there is one. taylor(f,var) approximates f with the Taylor series expansion of f up to the fifth order at the point var = 0.
Specify Expansion Point :
Find the Taylor series expansions at x = 1 for these functions. The default expansion point is 0. To specify a different expansion point, use 'ExpansionPoint':
syms x
taylor(log(x), x, 'ExpansionPoint', 1)
ans =
x - (x - 1)^2/2 + (x - 1)^3/3 - (x - 1)^4/4 + (x - 1)^5/5 - 1
Specify Truncation Order :
The default truncation order is 6.
syms x
f = log(x);
t6 = taylor(f, x);
Use 'Order' to control the truncation order. For example, approximate the same expression up to the orders 8.
syms x
taylor(log(x), x, 'ExpansionPoint', 1, 'Order', 8);
I am new to matlab and want to calculate something like f(x)/f'(x). I want the user to input the function f(x), the parameter x and a value of x (suppose 5,so that I can evaluate f(5)/f'(5)) .Please suggest what I should do.
One approach is to use symbolic variables
function [ val ] = func( fun, num )
symfun = sym(fun);
dsymfun = diff(symfun);
y = symfun/dsymfun;
val = subs(y, num);
end
and then call it
e.g.
value = func('x^2', 5)
value =
5/2
Otherwise, you can provide your input as a symbolic variable:
function [ val ] = func( fun, num )
dfun = diff(fun);
y = fun/dfun;
val = subs(y, num);
end
and then write
syms x;
func(x^2, 5)
You can do this using polyder and polyval as follows:
f = input('Enter f(x): '); %e.g; Enter [1 3 4] if f(x)= x^2 + 3*x + 4
df = polyder(f); %f'(x)
x= input('Enter x: '); %Enter the value of 'x' e.g 5
fx_dfx= polyval(f,x)/ polyval(df,x) %f(x)/f'(x)
If you have Symbolic Math Toolbox, you can also do this using:
syms x; %Creating a symbolic variable x
f = input('Enter f(x): '); %Enter f(x) e.g: x^2 + 3*x + 4
f(x)= f; %Converting sym to symfun
df(x) = diff(f) %f'(x)
x_val= input('Enter x: '); %Enter the value of 'x' e.g 5
fx_dfx = double(f(x_val)/df(x_val)) %f(x)/f'(x)
I am using ilaplace transform in matlab to compute the inverse Laplace transform of a function, however, I meet a strange error I cannot handle. Here is the output from matlab command line, where >> is the prompt. (s and t are syms symbolic variables)
>> N
N =
-(2.7071747341794232778783099808678e-34*(3.5938879023008902403763863659998e33*s - 68267793397927900578281423804583.0))/s^2
>> ilaplace(N)
Error using mupadmex
Error in MuPAD command: The argument is invalid. [_concat]
Evaluating: partfrac
Error in transform (line 74)
F = mupadmex('symobj::vectorizeSpecfunc', f.s, x.s, w.s, trans_func,
'infinity');
Error in sym/ilaplace (line 31)
F = transform('ilaplace', 's', 't', 'x', L, varargin{:});
>> ilaplace(-(2.7071747341794232778783099808678e-34*(3.5938879023008902403763863659998e33*s - 68267793397927900578281423804583.0))/s^2)
ans =
(23990078920530555628928726307903*t)/1298074214633706907132624082305024 - 4933332333844707562298796153537/5070602400912917605986812821504
The prblem is, if I directly invoke ilaplace(N), I have an error. However, if I pass the expression to ilaplace, I have the right answer. I don't know why.
EDIT: The complete code file is as follows.
syms s t;
syms s positive;
syms t positive;
tau = 4.2562313045743237429846099474892;
V_fitted = 1 - 1.015*exp(-0.0825*t); % CDF function
V_fitted_right_shift = subs(V_fitted, t, t - tau); % right shift CDF by tau
lambda_out = 1 / ( tau + int((1 - V_fitted_right_shift), [tau, Inf]) ); %arrival rate of the process
K = 4; %number of processes
V_CDF_superposed = 1 - (1 - V_fitted_right_shift)*(lambda_out * int((1-V_fitted_right_shift), [t-tau, Inf]))^(K - 1); %new CDF
FV_CDF_superposed = vpa(V_CDF_superposed);
Laplace_V_CDF_superposed = laplace(FV_CDF_superposed); %laplace transform
N = Laplace_V_CDF_superposed/(1 - s * Laplace_V_CDF_superposed);
N = vpa(N);
N = simplify(N);
N_t = ilaplace(N);