Writing this Mathcad equation in Matlab

hi, Im trying to convert an equation from mathcad into matlab but am struggling. the equation is:

 Accepted Answer

Try this:
psi = 0.142;
Io = 1;
f_vir2 = @(r) 4*Io * abs(integral(@(rho)besselj(0, pi*rho*r*psi).*rho, 0, 1)).^2;
r = pi;
test_call = f_vir2(r)

8 Comments

Thankyou Jason
As always, my pleasure.
If you are new to MATLAB, see the documentation on Anonymous Functions (link) to understand those I used here.
Hello, Im trying to plot the functional form as follows:
psi = 0.142;
Io = 1;
f_vir = @(r) 4*Io * abs(integral(@(rho)besselj(0, pi*rho*r*psi).*rho, 0, 1)).^2;
r = linspace(-20,20);
axes(handles.axes1); cla reset
plot(f_vir(r))
But get the following error:
Error using *
Inner matrix dimensions must agree.
Error in PSF>@(rho)besselj(0,pi*rho*r*psi).*rho (line 167)
f_vir = @(r) 4*Io * abs(integral(@(rho)besselj(0, pi*rho*r*psi).*rho, 0, 1)).^2;
Error in integralCalc/iterateScalarValued (line 314)
fx = FUN(t);
Error in integralCalc/vadapt (line 132)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
f_vir = @(r) 4*Io * abs(integral(@(rho)besselj(0, pi*rho*r*psi).*rho, 0, 1,'ArrayValued',true)).^2;
plot(r,f_vir(r))
Thankyou. whats the reason for "ArrayValued", "true" Jason
You call "f_vir" with an array of values for r, "integral" usually calls "f_vir" with an array of values for rho. This would usually make multiplications of the form "r*rho" within "f_vir" incompatible. To avoid this conflict in lengths, the "ArrayValued=true" option tells "integral" to call "f_vir" only with a single scalar for "rho".
You could also use
r=linspace(-20,20);
for i=1:numel(r)
f_vir_array(i)=f_vir(r(i));
end
plot(r,f_vir_array)
without the "ArrayValued=true" option.
Best wishes
Torsten.
Thankyou again
@Torsten — Thank you.
@Jason — Our pleasure.

Sign in to comment.

More Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Asked:

on 22 Mar 2018

Commented:

on 23 Mar 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!