1 Introduction

Compact stars are the final stage of stellar evolution when the radial pressure, caused by the nuclear fusion in the inner core, is no longer able to oppose the gravitational forces. Gravitational compact objects involve white dwarfs, neutron stars, and black holes. The interior structure as well as the physical properties of compact stars are studied adopting the Einstein field equations of general relativity (GR). Schwarzschild presented a first solution capable of describing the interior of the compact body in hydrostatic equilibrium [1].

The stellar core was first assumed to be a fluid with equivalent radial and tangential pressures [2]. However, the chances of generating anisotropies in compact stars are considerably higher due to the existence of strong interactions between particles. In this situation, the inner regions become non-uniform as discussed in [3]. Specifically, it is the fundamental nature of particles which can generate anisotropies. As a result, anisotropic compact stars can become more compact than isotropic stars. The consequences of local variations of relative inner fields were discussed using the equation of state and some interesting results were presented in [4]. Ruderman suggested that a nuclear system with very high densities indicates that anisotropic pressure can emerge in compact stars [5]. In such a case, the radial and tangential pressures are different within the stellar core. These problems can find room in the context of modified gravity.

Due to the issue that GR is not capable to address the whole gravitational phenomenology from ultraviolet to infrared scales, modified and extended theories of gravity have been proposed as effective approaches towards quantum gravity, from one side, and towards astrophysics and cosmology from the other side. These theories are considered very useful to investigate the expansion of the universe as well as the dark energy and dark matter issues which could be explained under the standard of geometrical effects considering curvature and torsion invariants into the gravitational action [6, 6,7,8, 8,9,10,11,12,13,14,15,16]. In particular, the so-called \(f\mathcal {(R)}\) gravity has gained special interest due to the fact that it is a straightforward generalization of the Hilbert–Einstein action of GR. Various models of \(f\mathcal {(R)}\) gravity have been studied to investigate problems like the late cosmic acceleration [17,18,19], the ghosts of gravitational field [20,21,22], the inflationary epoch [23] and the whole expansion of the universe [24,25,26]. In \(f\mathcal {(R)}\) gravity, several basic results have been achieved like the categorizations of singularities, the causal structure and the classification of energy conditions [27,28,29,30,31].

Using such modified theories, it is possible to explain the collapse and stability of astrophysical objects which escape the standard picture of GR. For example the supermassive compact object resulting from the gravitational wave event GW190814 can be framed and interpreted in the context of \(f({\mathcal {R}})\) gravity (see [32] and references therein).

In this framework, the study of compact stars has recently become an interesting research topic because observations are reporting several anomalous objects which cannot be explained by GR and standard equations of state. For example, anisotropic compact stars can be analyzed considering GR with a cosmological constant [33]. The compact structure can be discussed considering various forms of the energy density and radial pressure [34]. Singularity-free solution for compact stars can be derived in [35]. The homotopy perturbations procedure can be applied to obtain stable structures [36], and perturbative models of \(f\mathcal {(R)}\) gravity have been applied in [37] to study neutron stars. Neutron stars with quarks cores can also be considered, and results can be physically consistent [38]. The quadratic and cubic forms of \(f\mathcal {(R)}\) were discussed using numerical methods in [39], while anisotropic compact stars in the context of \(f\mathcal {(R)}\) gravity were studied in [40]. Neutron and quark stars are studied in the framework of \(f\mathcal {(R)} = {\mathcal {R}}+ \alpha {\mathcal {R}}^2\) while the Brans–Dicke theory is taken into account in [41]. In [42], quark star models are derived with an equation of state in the non-perturbative form of \(f\mathcal {(R)}\) gravity.

Besides these results, it is worth noticing that several modified gravities are receiving much attention to cure the shortcomings of GR like the cosmic accelerated expansion, the flat rotation curves of galaxies, possible wormhole solutions and other phenomena [43,44,45,46,47,48]. For example, a quadratic function of the Ricci scalar was studied by Starobinsky to address the inflationary behavior of early universe [49]. It was shown that higher-order \(f\mathcal {(R)}\) gravity can solve issues related to supermassive neutron stars [37, 42, 50,51,52]. The easiest way to prescribe \(f\mathcal {(R)}\) gravity is assuming an analytic function whose first order is the Ricci scalar. In general, the equations of motion of \(f\mathcal {(R)}\) are higher-order in derivatives and supply substantial classes of solutions that are different from GR. In the framework of \(f\mathcal {(R)}\) gravity, the dynamical behavior of matter and curvature fields has been studied in several works. See, for example, [53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70].

In particular, spherically symmetric vacuum black hole solutions in \(f\mathcal {(R)}\) have been derived in [71,72,73,74]. Furthermore, from the existence of Noether symmetries, it is possible to find out other spherically symmetric solutions [75, 76]. Using the same technique, it is possible to achieve also axially symmetric black hole solutions [77]. Non-trivial spherically symmetric black hole solutions for specific classes of \(f\mathcal {(R)}\) are derived also in [78,79,80]. Due to the higher-order curvature terms in \(f\mathcal {(R)}\), one can discuss strong gravitational fields in local objects. In this framework, many researches concentrate on the study of spherically symmetric black holes [81,82,83,84,85,86,87] and neutron stars [88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108].

Finally, it is worth noticing that \(f\mathcal {(R)}\) gravity can be recast as scalar-tensor gravity [109] with a scalar potential of gravitational origin so the further degrees of freedom with respect to GR result as scalar field counterparts in the Einstein field equations [110,111,112,113].

The aim of the present paper is deriving a spherically symmetric inner solution assuming general forms of the Ricci scalar and the \( f(R) \) function. After, we want to test the physical reliability of this solution. According to the procedure, it is possible to construct self-consistent compact stellar models capable of describing anisotropic neutron stars.

The layout of the paper is the following: In Sect. 2, a short summary of \(f\mathcal {(R)}\) gravity is reported. In Sect. 3, we apply the field equations of \( f(R) \) to a spherically symmetric metric with two different potentials for time and radial components. Assuming the form of radial component as well as of the first derivative of \(f\mathcal {(R)}\), we are able to obtain the time component of the metric and the \(f(\mathcal R)\) function. Starting from this information, we derive the density, the radial and the tangential pressures. In Sect. 4, we impose boundary conditions to match the interior with the exterior solution. We succeed in matching the constants characterizing the solution by a compactness parameter C which satisfies the constrain \(C<<0.5\). In Sect. 5, we discuss the physical conditions to show that the model is consistent with a realistic compact star. In Sect. 6, the stability, adopting the Tolman–Oppenheimer–Volkoff (TOV) approach and the adiabatic index, is discussed. We show that the model satisfies both these conditions. The results are matched with observational data in Sect. 7 where we draw the conclusions.

2 A summary of f(R) gravity

In 4-dimension, the action of \(f\mathcal {(R)}\) gravity is [114,115,116,117,118,119,120,121,122,123,124,125]:

$$\begin{aligned} {\mathop {{\mathcal {E}}}}:= & {} \frac{1}{2\kappa } \int d^4x \sqrt{-g} f\mathcal {(R)}\nonumber \\&+\frac{1}{2\kappa } \int d^4x \sqrt{-g} {{\mathcal {L}}_M}(g_{\mu \nu },\xi ), \end{aligned}$$
(1)

with the gravitational coupling \({\displaystyle \kappa =\frac{8\pi G}{c^4}}\), where G is the Newtonian constant, c is the speed of light and g is the determinant of the metric. Here \(f({\mathcal {R}})\) is an arbitrary function of the Ricci scalar \({\mathcal {R}}\) and \( {{\mathcal {L}}_M}(g_{\mu \nu },\xi )\) is the action of generic matter fields \(\xi \) which are minimally coupled to the metric \(g_{\mu \nu }\). Clearly, GR is recovered for \(f({\mathcal R})={{\mathcal {R}}}\).

The variations of action (1) w.r.t. the metric tensor \(g_{\mu \nu }\) gives the non-vacuum field equations [126]

$$\begin{aligned} {\mathop {{\mathcal {E}}}}_{\mu \nu }:= & {} {\mathcal {R}}_{\mu \nu } f_{{\mathcal {R}}}-\frac{1}{2}g_{\mu \nu }f\mathcal {( R)}\nonumber \\&+[g_{\mu \nu }\Box -\nabla _\mu \nabla _\nu ]f\mathcal { }_{_{\mathcal {R}}} -\kappa {{\mathcal {T}}}_{\mu \nu }\equiv 0, \end{aligned}$$
(2)

where \(\Box \) is the d’Alembert operator and \(\displaystyle f\mathcal { }_{_\mathcal { R}}=\frac{ {df}}{d{\mathcal {R}}}\). Here \( {{\mathcal {T}}}_{\mu \nu }\) is the energy-momentum tensor, defined as

$$\begin{aligned} {{\mathcal {T}}}_{\mu \nu } =-\frac{2}{\sqrt{-g}}\frac{\delta \mathcal{L_M}}{\delta g^{\mu \nu }}. \end{aligned}$$
(3)

Field Eq.  (2) have the following trace:

$$\begin{aligned} {\mathop {{\mathcal {E}}}}:=3\Box f\mathcal { }_{_\mathcal { R}}+{\mathcal {R}}f\mathcal { }_{_{\mathcal {R}}}-2f\mathcal {(R)} -\kappa {{\mathcal {T}}}\equiv 0 . \end{aligned}$$
(4)

From Eq. (4), one can obtain \(f\mathcal {(R)}\) in the form:

$$\begin{aligned} f\mathcal {(R)}=\frac{1}{2}\Big [3\Box {f_{{\mathcal {R}}}} +{\mathcal {R}}{f_{\mathcal {R}}}-\kappa {{\mathcal {T}}}\Big ]. \end{aligned}$$
(5)

Using Eq. (5) in Eq. (2) we get [127]

$$\begin{aligned} {\mathop {{\mathcal {E}}}}_{\mu \nu }:= & {} {\mathcal {R}}_{\mu \nu } F-\frac{1}{4}g_{\mu \nu }{\mathcal {R}}F\nonumber \\&+\frac{1}{4}g_{\mu \nu }\Box F -\nabla _\mu \nabla _\nu F-\kappa \Big ({{\mathcal {T}}}_{\mu \nu } -\frac{1}{4}g_{\mu \nu }{{\mathcal {T}}}\Big ) , \end{aligned}$$
(6)

where

$$\begin{aligned} F= & {} f_{\mathcal {R}}=\frac{ {df}}{d{\mathcal {R}}}=\frac{ {df}}{dr}\frac{dr}{d{\mathcal {R}}}. \end{aligned}$$
(7)

The energy-momentum tensor \({{\mathcal {T}}}_{\mu \nu }\) can be assumed with the following anisotropic form

$$\begin{aligned} { {\mathcal {T}}}_\mu {}^\nu {}=\Big (\frac{p_{t}}{c^2}+\epsilon \Big )u_\mu u^\nu +p_{_{_t}}\delta _\mu {}^\nu +(p-p_{_{_t}})\varepsilon _\mu \varepsilon ^\nu , \end{aligned}$$
(8)

where \(u_\mu \) is a time-like vector defined as \(u^\mu =[c,0,0,0]\) and \(\varepsilon _\mu \) is the unit space-like vector in the radial direction. In this study \(\epsilon \) is the energy-density, p and \(p_t\) are the radial and the tangential pressures respectively. The energy-momentum tensor takes the diagonal form \({ {\mathcal T}}_\mu {}^\nu {}= diag(-c^2\epsilon ;\,p;\,p_{_{_t}};\, p_{_{_t}})\).

3 Compact stars in \(f\mathcal {(R)}\) gravity

In order to consider relativistic compact stars in \(f({{\mathcal {R}}})\) gravity, let us assume the following spherically symmetric space-time:

$$\begin{aligned} ds^2= & {} -e^{a(r)}dt^2+e^{-b(r)}dr^2+r^2d\Omega , \quad {\text{ with }} \quad d\Omega \nonumber \\= & {} (d\theta ^2+\sin ^2d\phi ^2), \end{aligned}$$
(9)

where a(r) and b(r) are unknown functions of the radius. The Ricci scalar of the metric (9) takes the form:

$$\begin{aligned} {\mathcal {R}(r)}=\frac{e^{-b}r[ra'b'-2ra''-ra'^2-4a'+4b'-4]+4}{2r^2},\nonumber \\ \end{aligned}$$
(10)

with \(a'=\frac{da}{dr}\), \(a''=\frac{d^2a}{dr^2}\) and \(b'=\frac{db}{dr}\). For the line–element (9), the non-vanishing components of field Eqs. (4) and (6) have the form:

$$\begin{aligned} {\mathop {{\mathcal {E}}}}_t{}^t= & {} \frac{e^{-a}[e^{a-b}\{Fr^2( 2a''-a'b'+a'^2)+4rF[ra'+rb'-1]+r^2[3a'F'-2F''+F'b']-4rF'\} +4e^{a}F]}{r^2}-\frac{8\pi G}{c^2}\epsilon ,\nonumber \\ {\mathop {{\mathcal {E}}}}_r{}^r= & {} \frac{e^{-b}[Fr^2( 2a''-a'b'+a'^2)-4F[ra'+rb'+1]-r^2[a'F'-6F''+3F'b']-4rF'+4e^{a}F]}{r^2}+\frac{8\pi G}{c^4} p,\nonumber \\ {\mathop {{\mathcal {E}}}}_\theta {}^\theta= & {} {\mathop {{\mathcal {E}}}}_\phi {}^\phi =\frac{F[4-e^{-b}(4-2r^2a''+r^2a'b'-r^2a'^2)] +e^{-b}[r^2a'b'+2r^2F''-r^2F'b'-4rF']}{r^2}-\frac{8\pi G}{c^4} p_{_{_t}},\nonumber \\ {\mathop {{\mathcal {E}}}}= & {} \frac{8\pi G}{c^4}[p+2p_{_{_t}}-c^2\epsilon ]+\frac{1}{r^2}\Big [e^{-b}[r^2(6F''-2Fa''-Fa'^2)+r[r\,F\, b'+3rF'-4F]a'\nonumber \\&+r\beta '[4F-3rF']+12rF'-4F)+4(F-f)\Big ]. \end{aligned}$$
(11)

Moreover, we introduce the characteristic density

$$\begin{aligned} \epsilon _\star \equiv \frac{c^2}{8\pi G R_0^2}, \end{aligned}$$
(12)

where \(R_0\) is a characteristic radius. It can be used to re-scale the density, the radial and the tangential pressure respectively. We get the dimensionless variables

$$\begin{aligned} {\hat{\epsilon }} = \frac{\epsilon }{\epsilon _\star }, \quad {\hat{p}} = \frac{p}{\epsilon _\star c^2}, \quad {\hat{p}}_t = \frac{p_t}{\epsilon _\star c^2}. \end{aligned}$$
(13)

Equation (11) are four non-linear differential equations in six unknown variables a, b, F \(\epsilon \), p and \(p_{_{_t}}\). Using \({\mathop {{\mathcal {E}}}}_r{}^r\) and \({\mathop {\mathcal { E}}}_\theta {}^\theta \), one can define the anisotropy parameter of stellar structure as:

$$\begin{aligned} \Delta (r)= & {} \frac{8\pi G}{c^4}\Big [p_{_{_t}}-p\Big ]\nonumber \\= & {} \frac{e^{-b}\Big (2Fr^2a''+4r^2F'' +r^2Fa'^2-r\,F\,a'[rb'+2]-2[rb'+2][rF'+F]\Big )+4F}{4r^2}, \end{aligned}$$
(14)

which reduces to \(\Delta (r)=0\) for isotropic systems. In order to find a physical solution of the above system of differential equations, let us assume the following forms for the radial component of metric potential \(g_{rr}\) and the \(f({\mathcal {R}})\) derivative function F as:

$$\begin{aligned} b(y)=c_0 y^2,\quad F(y)=1+c_1y^2. \end{aligned}$$
(15)

This choice is physically motivated for the following reasons. Here y is a dimensionless variable

$$\begin{aligned} y\equiv \frac{r}{l} \in [0, 1], \end{aligned}$$
(16)

and we assume the star can extend up to the maximal radius \(r =l\). The parameters \(c_0\) and \(c_1\) are dimensionless.They will be determined later. Equation (15) show that when \(c_1=0\), we recover GR. In other words, the constant \(c_1\) is responsible for the deviation from GR. Using Eqs. (15) into (14), we get the metric potential \(g_{tt}\) as

$$\begin{aligned} a(y)=2ln\Big (\frac{c_0c_2+c_3e^{c_0y^2/2}}{2c_0}\Big ), \end{aligned}$$
(17)

where \(c_2\) and \(c_3\) are given by combinations of the parameters \(c_{0}\), \(c_{1}\) and l. The anisotropy parameter \(\Delta (y)\) has the form

$$\begin{aligned} \Delta (y)= & {} \frac{1+y^2l^2c_1-[1+y^2l^2c_1+c_0y^2l^2+3y^2l^4c_0c_1]e^{-c_0y^2l^2}}{y^2l^2}.\nonumber \\ \end{aligned}$$
(18)

Using Eqs. (15) and (17) in Eq. (11), we get the exact form of energy, radial and tangential pressures that we report in Appendix A.

Inserting Eq. (15) in the last equation of system (11), the trace of the field Eqs. (6), we get a very lengthy differential equation in the unknown \(f({\mathcal {R}})\). We report the full equation in Appendix C. Developing such a differential equation up to the first order in \(\mathcal{O}\Bigg (\frac{1}{y}\Bigg )\), we get all the information we need for our purposes. It is

$$\begin{aligned} \frac{1}{2c_0{}^2ym^2}\left( 3mm_1{}^2f'''+15c_0m_1m_2f'\right) \approx 0, \end{aligned}$$
(19)

where \(m=5c_0{}^2c_2{}^2+7c_0c_2c_3-c_3{}^2\), \(m_1=c_3+c_0c_2\) and \(m_2=173c_0{}^3c_2{}^3+138c_0{}^2c_2{}^2c_3{}+48c_0c_2c_3{}^2-8c_3{}^3\). The solution of the above differential equation takes the form

$$\begin{aligned} f(y)=c_4+c_5\sin \Bigg (\frac{\sqrt{c_0m_2}y}{\sqrt{3m\,m_0}}\Bigg )+c_6\cos \Bigg (\frac{\sqrt{c_0m_2}y}{\sqrt{3m\,m_0}}\Bigg ),\nonumber \\ \end{aligned}$$
(20)

where \(c_4\), \(c_5\) and \(c_6\) are integration constants. For \(c_{5}\), the solution (20) is compatible with F(y), given by Eq. (15), up to the leading order. Equations of density, radial and tangential pressures show that, for \(c_1=0\), we return to GR. This is due to the fact that, for \(F=1\), we have \(f({\mathcal {R}})={\mathcal {R}}\). This means that differences with GR come from \(c_1\ne 0\). See Appendix A.

Using Eqs. (15) and (17), we get the Ricci scalar, up to the leading order, in the form

$$\begin{aligned} {\mathcal {R}}\approx & {} \frac{6c_0{}^2}{m_1}-\Bigg [\frac{6c_0{}^3c_3}{m_1{}^2}+\frac{c_0{}^2[c_0c_3+5c_0{}^2-c_3{}^2]}{m_1{}^2}\Bigg ]y^2+\cdots \, ,\nonumber \\&\,\, \text{ which } \text{ leads } \text{ to } \,\, y\approx \pm \sqrt{\frac{6c_0{}^2m_1-{\mathcal R}\,m_1{}^2}{6c_0{}^3c_3+c_0{}^2[c_0c_3+5c_0{}^2-c_3{}^2]}}+\cdots .\nonumber \\ \end{aligned}$$
(21)

Using Eq. (21) into Eq. (20), we get the explicit form of \(f({{\mathcal {R}}}) \), that is

$$\begin{aligned} f({\mathcal R})=c_4+c_6\cos \left( \frac{\sqrt{m_2(6c_0{}^2m_1-{\mathcal R}\,m_1{}^2)}}{\sqrt{3m\,m_0({6c_0{}^2c_3{+}c_0[c_0c_3{+}5c_0{}^2{-}c_3{}^2]})}}\right) .\nonumber \\ \end{aligned}$$
(22)
Fig. 1
figure 1

Density, radial and transverse pressures when \(c_1=0\) and \(c_1=0.5\)

4 Matching conditions

The solution given in Appendix A has a non-trivial Ricci scalar as shown by Eq. (21). Let us match it with the exterior solution. To this aim, we are going to match solution given in Appendix A with the one presented in [80] that we report here:

$$\begin{aligned} ds^2= & {} -\Big (\frac{1}{2}-\frac{2MG}{c^2r}\Big )dt^2\nonumber \\&+\Big (\frac{1}{2}-\frac{2MG}{c^2r}\Big )^{-1}dr^2+r^2d\Omega ^2, \end{aligned}$$
(23)

where M is the total stellar mass and \(4MG<c^2r\). We have to match the interior spacetime metric (9), using Eqs. (15) and (17), with the exterior Schwarzschild spacetime metric given by Eq. (23) at the boundary of the star, \(r =l\). The continuity of the metric functions across the boundary \(r =l\) gives

$$\begin{aligned} a(r=l)= & {} ln\Bigg (\frac{1}{2}-\frac{2GM}{c^2l}\Bigg ), \nonumber \\ b(r=l)= & {} ln\Bigg (\frac{1}{2}-\frac{2GM}{c^2l}\Bigg )^{-1}. \end{aligned}$$
(24)

Using the above conditions, we get the constraints on the constants \(c_0\), \(c_2\). These constants take the form

$$\begin{aligned} c_0= & {} -ln\Bigg (1/2-C\Bigg ), \nonumber \\ c_2= & {} \frac{c_3+2\Bigg (1/2-C\Bigg )\ln \Bigg (1/2-C\Bigg )}{\sqrt{\Bigg (1/2-C\Bigg )}\ln \Bigg (1/2-C\Bigg )}, \end{aligned}$$
(25)

where \(C=\frac{2GM}{c^2l}\). Equation (25) shows that the compactness parameter must be \(C<0.5\). This is also a consistency condition.

Fig. 2
figure 2

Anisotropy \(\Delta (y)\) and anisotropy force when \(c_1=0\) and \(c_1=0.5\)

Fig. 3
figure 3

The gradient of density, radial and transverse pressures when \(c_1=0\) and \(c_1=0.5\)

5 The physical viability of solution

Now we are going to test if the interior solution, reported in Appendix A, can describe a realistic physical star. To this aim, we have to derive some necessary conditions which have to hold for any realistic star. They are the following.

5.1 Energy–momentum tensor

It is well-known that, for a realistic interior solution, the energy–density, the radial and transverse pressures must be positively defined. Moreover, all the components of the energy-momentum tensor should be finite at the center of the star then become decreasing in the direction of the surface of the stellar structure and the radial pressure should exceeds the tangential one.Footnote 1 The plots of the energy-momentum components, density, radial and transverse pressures are shown in Fig. 1. The values of parameters used in the figure are: \({\hat{\epsilon }}(y=0)_{c_1=0,c_3=0.5}=4.481840660\), \({\hat{\epsilon }}(y=0)_{c_1=0.5,c_3=0.5}=3.731840660\), \({\hat{p}}(y=0)_{c_1=0,c_3=0.5}={\hat{p}}_t(y=0)_{c_1=0,c_3=0.5}=1.493946887\), \({\hat{p}}(y=0)_{c_1=0.5,c_3=0.5}={\hat{p}}_t(y=0)_{c_1=0,c_3=0.5}=1.243946887\). Figure 1 shows that the density, radial and tangential pressures are decreasing towards the stellar surface. Moreover Fig. 1 shows that the values of the components of the energy momentum at the center, in case \(c_1=0\), are greater than those for \(c_1\ne 0\). We can also deduce, from Fig. 1, that density, radial and tangential pressures converge to the surface of the star more rapidly in case \(c_1=0\) than in the case of \(c_1=0.5\). Finally, it is easy to see that, when \(c_1=0\), one gets \({\hat{p}}={\hat{p}}_t\) at the center however, as we reach the surface of the star, we can see that \({\hat{p}}_t\ge {\hat{p}}\).

In Fig. 2, we show the behavior of the anisotropy parameter which is defined as \(\Delta (y)={\hat{p}}_t-{\hat{p}}\). Also Fig. 2 shows that the anisotropic force is positive. This means that a repulsive force, due to \(p_t\ge p\), is present.

Fig. 4
figure 4

Radial, transverse sound speeds and \({|}v_r{}^2-v_t{}^2{|}\) for \(c_1=0\) and \(c_1=0.5\)

Fig. 5
figure 5

DEC for \(c_1=0\) and \(c_1=0.5\)

5.2 Causality

In order to show the behavior of sound velocities, we must calculate the gradient of energy–density, radial and transverse pressures that take the forms given in Appendix B. Let us analyze the gradient of energy-momentum tensor components reported in Appendix B. The asymptotic forms are

$$\begin{aligned}&{\hat{\epsilon }}'\approx \frac{c_0[26c_1c_3{}^2{-}11c_0c_3{}^2{+}40c_0c_1c_2c_3{-}13c_0{}^2c_2c_3{}^2{-}5c_0{}^3c_2{}^2+14c_0{}^2c_1c_2{}^2]}{2[c_1{}+c_0c_2]},\nonumber \\&{\hat{p}}'\approx \frac{c_0[14c_1c_2{}^2-5c_0c_3{}^2+24c_0c_1c_2c_3-7c_0{}^2c_2c_3-3c_0{}^3c_2{}^2+10c_0{}^2c_1c_2{}^2]}{2[c_1{}+c_0c_2]},\nonumber \\&{\hat{p}}'_t\approx \frac{c_0[6c_1c_3{}^2-3c_0c_3{}^2+8c_0c_1c_2c_3-3c_0{}^2c_2c_3-c_0{}^3c_2{}^2+2c_0{}^2c_1c_2{}^2]}{2[c_1{}+c_0c_2]},\nonumber \\ \end{aligned}$$
(26)

which give a negative gradient of the energy-momentum components provided that \(c_0>c_1\ge c_2>c_3\). This condition is satisfied according to the choice in footnote 1. The behavior of density, radial and tangential pressure gradients are shown in Fig. 3. To ensure that causality conditions, either for the radial and transverse sound speeds \(v_r{}^2\) and \(v_\bot {}^2\), have values less than the speed of light, we use equations reported in given in Appendix B and get

$$\begin{aligned}&v_r{}^2=\frac{{\hat{p}}'}{{\hat{\epsilon }}'} \nonumber \\&\quad \approx \frac{14c_1c_3{}^2-5c_0c_3{}^2-7c_0{}^2c_2c_3+24c_0c_1c_2c_3-3c_0{}^3c_2{}^2+10c_0{}^2c_1c_2{}^2}{26c_1c_3{}^2-11c_0c_3{}^2-13c_0{}^2c_2c_3+40c_0c_1c_2c_3-5c_0{}^3c_2{}^2+14c_0{}^2c_1c_2{}^2}, \nonumber \\&v_t{}^2=\frac{{\hat{p}}'_t}{{\hat{\epsilon }}'}\nonumber \\&\quad \approx \frac{6c_1c_3{}^2-3c_0c_3{}^2-3c_0{}^2c_2c_3+8c_0c_1c_2c_3-c_0{}^3c_2{}^2+2c_0{}^2c_1c_2{}^2}{26c_1c_3{}^2-11c_0c_3{}^2-13c_0{}^2c_2c_3+40c_0c_1c_2c_3-5c_0{}^3c_2{}^2+14c_0{}^2c_1c_2{}^2}.\nonumber \\ \end{aligned}$$
(27)

Equation (27) show that both the conditions \(1>v_r{}^2>0\) and \(1>v_t{}^2>0\) hold using the choice in footnote 1. The behaviour of the radial and tangential sound speeds are shown in Fig. 4a, b.

The appearance of non–vanishing total radial force with different signs in different regions of the fluid is called gravitational cracking when this radial force is directed inward in the inner part of the sphere for all values of the radial coordinate r between the center and some value beyond which the force reverses its direction [128]. In Ref. [129], it is stated that a simple requirement to avoid gravitational cracking is \(0< v_r{}^2-v_t{}^2<c^2\). In Fig. 4c, we show that the solution given in Appendix A is stable against cracking for \(c_1=0\) and \(c_1=0.5\).

5.3 Energy conditions

Energy conditions are considered an important test for non–vacuum solutions. To satisfy the dominant energy condition (DEC), we have to prove that \({\hat{\epsilon }}-{\hat{p}} >0\) and \({\hat{\epsilon }}-{\hat{p}}_t>0\). As shown in Fig. 5a, b, DEC is satisfied for suitable choices of parameter \(c_1\). Furthermore, the weak energy condition (WEC), \({\hat{\epsilon }}+{\hat{p}}>0\) \({\hat{\epsilon }}+{\hat{p}}_t>0\) and the strong energy condition (SEC), \({\hat{\epsilon }}-{\hat{p}}-2{\hat{p}}_t>0\) are satisfied as shown in Fig. 6a–c.

Fig. 6
figure 6

WEC and SEC of the solution given in Appendix A for \(c_1=0\) and \(c_1=0.5\)

Fig. 7
figure 7

The gravitational mass and compactification factor of solution given in Appendix A for \(c_1=0\) and \(c_1=0.5\)

Fig. 8
figure 8

The radial and transverse EoS when \(c_1=0\) and \(c_1=-0.01\)

5.4 Mass–radius relation

The compactification factor u(r) is defined as the ratio between the mass and radius. It has a key role to understand physical properties of compact objects. Starting from the solution given in Appendix A, we can define the gravitational mass as

$$\begin{aligned} M(y)= & {} {\int _0}^y \hat{{\hat{\epsilon }}} \zeta ^2 d\zeta =\frac{1}{8yc_0{}^{5/2}c_2{}^2}\Bigg [4yc_0{}^{5/2}c_2{}^2\nonumber \\&\times \int \frac{3c_3{}^3(2y^2c_1+1)}{c_0c_2{}^2(c_3+c_0c_2e^{-c_0y^2/2})}dr\nonumber \\&-24c_0{}^{3/2}c_1c_2c_3y^2 e^{-c_0y^2/2}\nonumber \\&+6c_0c_2c_3\sqrt{2}(c_0+2c_1)\,\sqrt{\pi }\,erf(y\sqrt{c_0/2})\nonumber \\&+2c_0^{5/2}c_2{}^2e^{-c_0y^2}[1-3c_1y^2]+yc_2{}^2(8c_0-5c_1)c_0{}^2\,\nonumber \\&\times \sqrt{\pi }\,erf(y\sqrt{c_0})+4c_0{}^{3/2}\{c_0c_2{}^2(y^2c_1-1)\nonumber \\&-2c_1c_2{}^2y^4-3c_3{}^2y^2\}\Bigg ], \end{aligned}$$
(28)

where erf(x) is the error function that is defined as

$$\begin{aligned} erf(x)=\frac{2}{\sqrt{\pi }}\int _0^x e^{-t^2}dt. \end{aligned}$$
(29)

The compactification factor u(r) is then defined as

$$\begin{aligned}&u(y)=\frac{M(y)}{l}. \end{aligned}$$
(30)

Using Eq. (28) into (30), one can get the explicit form of the compactification factor. The behavior of the gravitational mass and the compactification factor are plotted in Fig. 7. Figure 7a shows that the gravitational mass increases when the radial coordinate increases while Fig. 7b shows that the compactification factor decreases for increasing radial coordinate.

5.5 Equation of state

The study of equation of state (EoS) for neutral compact stellar objects is reported in [130] . In that case, EoS is linear. In the present case, we will show that EoS cannot non-linear. To investigate this issue, we define the radial and transverse EoS as:

$$\begin{aligned} \omega _r= & {} \frac{{\hat{p}}}{{\hat{\epsilon }}}, \quad \omega _t=\frac{{\hat{p}}_t}{{\hat{\epsilon }}}, \end{aligned}$$
(31)

where \(\omega _r\) and \(\omega _t\) are the radial and transverse EoS. Using equations in Appendix A, one can calculate the analytical form of \(\omega _r\) and \(\omega _t\) whose behaviors are plotted in Fig. 8. As Fig. 8a, b show, EoS are non-linear because of the contribution of the higher–order curvature terms.

Fig. 9
figure 9

The TOV of solution when \(c_1=0\) and \(c_1=0.5\)

Fig. 10
figure 10

The adiabatic index of solution given in Appendix A for \(c_1=0\) and \(c_1=0.5\)

6 Stability of the model

In order to study the stability problem we are going to use two different procedures; the first one is the Tolman–Oppenheimer–Volkoff (TOV) equation and the second is the study of the adiabatic index.

6.1 The Tolman–Oppenheimer–Volkoff equation

To study the stability of solution in Appendix A, let us assume the hydrostatic equilibrium by using the TOV equation [131, 132] as presented in [133]. It is

$$\begin{aligned} \frac{2[{\hat{p}}_t-{\hat{p}}]}{y}- & {} \frac{M_g(y)[{\hat{\epsilon }}(y) +{\hat{p}}_t(y)]e^{[a(y)-b(y)]/2}}{y}\nonumber \\- & {} \frac{d{\hat{p}}(y)}{y}=0, \end{aligned}$$
(32)

where \(M_g(y)\) is the gravitational mass calculated at the radius y. It has the following equation

$$\begin{aligned} M_g(y)= & {} 4\pi {\int _0}^y\Big ({{{\mathcal {T}}}_t}^t{-}{{{\mathcal {T}}}_r}^r{-}{{{\mathcal {T}}}_\theta }^\theta {-}{{{\mathcal {T}}}_\phi }^\phi \Big )y^2e^{[a(y)+b(y)]/2}dy\nonumber \\= & {} \frac{y a' e^{[b(y)-a(y)]/2}}{2}, \end{aligned}$$
(33)

Inserting Eq. (33) into (32), we get

$$\begin{aligned}&\frac{2({\hat{p}}_t-{\hat{p}})}{y} -\frac{d{\hat{p}}}{dy}-\frac{a'[{\hat{\epsilon }}(y)+{\hat{p}}(y)]}{2}\nonumber \\&\quad =F_g+F_a+F_h=0, \end{aligned}$$
(34)

where \(F_g=-\frac{a'[{\hat{\epsilon }}+{\hat{p}}]}{2}\), \(F_a=\frac{2({\hat{p}}_t-{\hat{p}})}{y}\) and \(F_h=-\frac{d{\hat{p}}(y)}{dy}\) are the gravitational, the anisotropic and the hydrostatic forces respectively. The behavior of TOV equation, for the model given in Appendix A, is shown in Fig. 9. This figure shows that hydrostatic and anisotropic forces are positive and dominated on the gravitational force which is negative to keep the system in static equilibrium either when \(c_1=0\) or \(c_1=0.5\).

6.2 The adiabatic index

The adiabatic index \(\gamma \) is defined as [134, 135]

$$\begin{aligned} \gamma =\left( \frac{{\hat{\epsilon }}+{\hat{p}}}{{\hat{p}}}\right) \frac{d{\hat{p}}}{d{\hat{\epsilon }}}. \end{aligned}$$
(35)

It allows to link the structure of a spherically symmetric static object and the EoS of the interior solution. It can be used to investigate the stability [136]. Any interior solution is stable if its adiabatic index is greater than 4/3 [137]. If \(\gamma =\frac{4}{3}\), then the isotropic sphere is in neutral equilibrium. Following Chan et al. [138], the stability condition of a relativistic anisotropic sphere, \(\gamma >\Gamma \), must be satisfied. Here it is

$$\begin{aligned} \Gamma= & {} \frac{4}{3}-\left\{ \frac{4({\hat{p}}-{\hat{p}}_t)}{3|{\hat{p}}'|}\right\} _{max}. \end{aligned}$$
(36)

The behaviour of the adiabatic index is shown in Fig. 10 which ensures the stability condition of our solution.

Table 1 The boundary density \(\rho _l\) and slope of the radial pressure reported for various observed objects. The density and the derivative of radial pressure are derived for the values \(c_1=0\) and \(c_1=0.5\) at the stellar boundary l assuming a nuclear density of the order \(10^{15}\,g/cm^3\)

7 Discussion and conclusions

In this study, we derived an anisotropic spherically symmetric solution in the framework of \(f\mathcal {(R)}\) gravity. To this aim, we used a line element containing two unknown functions and applied it to the field equations of \(f\mathcal {(R)}\) deriving four non-linear differential equations: three of them are the field equations and the fourth one is the trace of the field equations. This system involves six unknown functions, two of them are the metric potentials, one is the form of \(f\mathcal {(R)}\) and the other are the density, and radial and tangential pressures. We solved these differential equations assuming the forms of the radial component of the metric and of the derivative of \(f\mathcal {(R)}\). Substituting these quantities in the anisotropy parameter, \(\Delta ={\hat{p}}_t-{\hat{p}}_r\), we get the time component of the metric. From these results, we obtained the form of \(f\mathcal {(R)}\), the density, radial, and tangential pressures. It is interesting to mention that our solution is characterized by a constant \(c_1\) which parameterize the deviation with respect to GR. Specifically, for \(c_1=0\), we return to GR.

Due to the fact that the obtained interior solution has a non-trivial form for the Ricci scalar, we match it with a spherically-symmetric exterior solution having also a non-trivial form of Ricci scalar. From this matching, it is possible to relate the mass and the maximal radius of the compact object with the compactness parameter, that is \(C=\frac{2GM}{c^2l}\) which must be \(C<<0.5\) for the physical consistency of the model. The further step is to see if this solution represents a realistic star. To this aim, we discussed the necessary conditions required by any physically consistent compact stellar object and show that our model satisfies all these conditions. In particular, we considered two cases: one for \(c_1=0\) and another for \(c_1=0.5\).

Finally, we checked if the model satisfies the stability conditions. To this aim, we derived the TOV equation and showed that it is in static equilibrium either when \(c_1=0\) or \(c_1=0.5\). We also studied the adiabatic index to complete the picture of the stability and showed that the model satisfies the adiabatic index \(\gamma >4/3\).

These results can be applied to the observational data of different millisecond pulsars with white dwarf companions and pulsars presenting thermonuclear bursts as reported in Table 1.

For these objects, it is possible to calculate the density at the center and at the surface and then derive the EoS at the center and at the surface. It is interesting to see that, in the case of millisecond pulsars with a white dwarf companion, and, in the case of pulsars presenting thermonuclear bursts, our solution is compatible with observations. From the observational data in literature, as reported in the Table 1, it is possible to derive the total mass and the radius of the given pulsar.

To conclude, we derived an interior solution in the framework of \(f\mathcal {(R)}\) assuming physically motivated conditions to obtain the form of \(f\mathcal {(R)}\). This solution has a non-trivial form of the Ricci scalar. The internal solution can be matched with the external one considering compacteness and anisotropy parameters. In a forthcoming paper, we will consider a similar situation assuming a charged interior solution.