Using Interpolation in Matlab to return a function - matlab

I've got a silly problem, I'm looking to take a few data points, fit a polynomial function through it and then differentiate that function to get that particular functions optimal point. As such I have done some reading online and I've used the Matlab 'spline' function. Here is some code:
a = [50; 100; 150;200;250;300;350]
b = [56;23;22;18;14;15;21]
y = spline(a,b)
But when I used diff(y) I get the following error:
??? Error using ==> diff
Function 'diff' is not supported for class 'struct'.
I'm not too familiar with Matlab, so any help would really be appreciated

As per comments:
y = polyfit(a,b,2)
syms x
g = y(1)*x^2 + y(2)*x + y(3)
diff(g)
and you get the derivative of the function g. Much thanks to the guys in the comment section!

Related

Definite integration using int command

Firstly, I'm quite new to Matlab.
I am currently trying to do a definite integral with respect to y of a particular function. The function that I want to integrate is
(note that the big parenthesis is multiplying with the first factor - I can't get the latex to not make it look like power)
I have tried plugging the above integral into Desmos and it worked as intended. My plan was to vary the value of x and y and will be using for loop via matlab.
However, after trying to use the int function to calculate the definite integral with the code as follow:
h = 5;
a = 2;
syms y
x = 3.8;
p = 2.*x.^2+2.*a.*y;
q = x.^2+y.^2;
r = x.^2+a.^2;
f = (-1./sqrt(1-(p.^2./(4.*q.*r)))).*(2.*sqrt(q).*sqrt(r).*2.*a-p.*2.*y.*sqrt(r)./sqrt(q))./(4.*q.*r);
theta = int(f,y,a+0.01,h) %the integral is undefined at y=2, hence the +0.01
the result is not quite as expected
theta =
int(-((8*461^(1/2)*(y^2 + 361/25)^(1/2))/5 - (461^(1/2)*y*(8*y + 1444/25))/(5*(y^2 + 361/25)^(1/2)))/((1 - (4*y + 722/25)^2/((1844*y^2)/25 + 665684/625))^(1/2)*((1844*y^2)/25 + 665684/625)), y, 21/10, 5)
After browsing through various posts, the common mistake is the undefined interval but the +0.01 should have fixed it. Any guidance on what went wrong is much appreciated.
The Definite Integrals example in the docs shows exactly this type of output when a closed form cannot be computed. You can approximate it numerically using vpa, i.e.
F = int(f,y,a,h);
theta = vpa(F);
Or you can do a numerical computation directly
theta = vpaintegral(f,y,a,h);
From the docs:
The vpaintegral function is faster and provides control over integration tolerances.

VHDL Code Generation using Matlab HDL Coder

I am sorry If I say some thing silly.Please forgive me:
I am trying to convert Matlab code(given below) to VHDL code,using HDL coder.It contains a function called sum.But when I try to convert the code it gives me error :
code generation only supports SumModes 'SpecifyPrecision' and
'KeepLSB' for 'SUM' when the size of the inputs can vary at run-time.
But the thing is I have never used functions before.Can any one please help me on it.How should change my code to convert it to VHDL.It would be really nice!
function y = fcn(n,y1,y2)
n=10;
x1c=zeros(2*n-1,1);
for i=1:2*n-1
if(i>n)
j1=1;
k1=2*n-i;
j2=i-n+1;
k2=n;
else
j1=n-i+1;
k1=n;
j2=1;
k2=i;
end
x1c(i)=sum((y1(j1:k1)).*y2(j2:k2));
end
x1c=flipud(x1c).';
y=x1c;
This is cross correlation of y1 and y2. y1 and y2 are two vectors of same length and n is length of y1. I am really stuck please help!
Thanks in advance!
#Haider: Take a look at
n=10;
y1 = 1:n;
y2 = n:-1:1;
x1c=zeros(1,2*n-1);
for i=1:2*n-1
y1_temp = zeros(1,2*n-1);
y2_temp = zeros(1,2*n-1);
if(i>n)
j1=1;
k1=2*n-i;
j2=i-n+1;
k2=n;
else
j1=n-i+1;
k1=n;
j2=1;
k2=i;
end
y1_temp(j1:k1) = y1(j1:k1);
y2_temp(j1:k1) = y2(j2:k2);
x1c(i)=sum(y1_temp.*y2_temp);
end
I compared the result with the Matlab xcorr function, and it seems the vector is reversed. Does this solve the error?

function in Julia equivalent to matlabfunction()?

I need a function equivalent to matlabfunction() of matlab, does anyone know if there is or how to do?
Function documentation in MatLab:
http://www.mathworks.com/help/symbolic/matlabfunction.html
I'm doing a translation of MatLab for Julia . This part that I am having difficulties:
syms x
dados = inputdlg({'P(x): ', 'Q(x): ', 'R(x): ', 'N: '},'Dados');
P = matlabFunction(sym(dados{1}),'vars',x,'file','px');
Thanks for any help.
If I understand what matlabfunction is, you don't need it in Julia.
Here is a MATLAB example:
syms x y
r = sqrt(x^2 + y^2);
ht = matlabFunction(sin(r)/r)
and here is the same in Julia:
julia> ht = (x,y) -> (r=sqrt(x^2 + y^2); sin(r)/r)
(anonymous function)
julia> ht(1,1)
0.6984559986366083
It looks like matlabfunction does some other things too, but you'd need to give a more concrete question for me to tackle that.
EDIT: This answer addressed question in its original form - an edit has changed it but hasn't added much clarification.

Solve trigonometrics equations in Matlab

I'm trying to solve for t trigonometric equations in Matlab, as i.e. 7*cos(t) + 5*sin(t) = 0. I would solve it as: sin(t)/cos(t) = -7/5 and I would find it as arctan(-7/5) = -0.9505.
I have tried to do it on matlab using solve function:
syms t
theta = solve(7*cos(t) + 5*sin(t)==0, t);
disp(theta);
But I get -(log(- 12/37 - (35*i)/37)*i)/2 instead of -0.9505. Could someone explain me why I get this answer from solve and how to get -0.9505?
The expression is the exact result, expressed symbolically (due to the use of syms).
To make Matlab display the result in the format your looking for use double(theta)
which should give you:
double(theta)
ans =
-0.9505

How do I symbolically integrate functions in matlab where I know the output must contain bessel functions?

I am working with the following function:
f(x) = a*(1+cos(3*x)) where a is constant/parameter.
y(x) = exp(-b*f(x)) where b is another constant.
I need to compute the definite integral integral(0,2pi)y(x)dx.
I am trying to implement another research paper for our research group. I know the output must contain modified bessel functions of the first kind which are functions of 'a'. Matlab simply refuses to evaluate this integral.
The following is my code(Matlab):
syms x;
syms a;
syms b;
f_x = a*(1+cos(3*x));
y_x = exp(-b*f_x);
z_x = int(y_x, x, 0, 2*pi)
Output:
Warning: Explicit integral could not be found.
z_x =
int(1/exp(a*b*(cos(3*x) + 1)), x = 0..2*pi)
Request your assistance in solving this! I am sure that the integral contains bessel functions like I(a), etc. at many places. Is there any pre-processing I need to do here? I kind of need this solution urgently. I appreciate quick responses which can atleast point in the right direction.
Solved!. It worked in mathematica, failed in Matlab.
Starting R2017b, this works. Use int.
>> syms x;
syms a;
syms b;
f_x = a*(1+cos(3*x));
y_x = exp(-b*f_x);
z_x = int(y_x, x, 0, 2*pi)
z_x =
2*pi*exp(-a*b)*besselj(0, a*b*1i)