error in logical calculation

2 visualizaciones (últimos 30 días)
yogeshwari patel
yogeshwari patel el 5 de Abr. de 2022
Comentada: Stephen23 el 30 de Sept. de 2023
syms x a
Y=sym(zeros(1));
Y(1)=0;
a=1/2
for i=1:4
if i==5
A=1
else
A=0
end
if i==4
B=1
else
B=0
end
Y(i+1)=simplify((gamma(a*(i-1)+1)/gamma((a*(i-1)+3/2))*(A-Y(i)+((2*B)/gamma(5/2)))));
end
disp(Y)
%%%%%%%%%%%%%%%%%%%%%
for i=4 ,the value of Y(5) should be 1 but it is showwing s2535301200456458897054207582575/2535301200456458802993406410752. If i work with pen and paper and simplify it again i get 1 .So what is the mistake in the code
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Question -2
syms x a
series(x)=sym(zeros(1));
Y=sym(zeros(1));
m=sym(1);
Y(1)=0;
Y(2)=a;
%N=input ('enter the number of coefficient required')
for k=1:6
Y(k+2)=((18*kroneckerDelta(k,m)-9*Y(k)))/(k*(k+1));
end
disp(Y)
for k=1:6
series(x)=series(x)+Y(k)*(power(x,k-1));
end
series % Find the series in terms of a and x
M=series(pi/2)-1
a=solve(M) % find the value of a
series
I want to put the value of a in series and want to evaluete the series in terms of x only. I used the command series but it is not working
[SL: edited to pull the prose in the middle of the two questions out of the code block.]
  1 comentario
Jan
Jan el 5 de Abr. de 2022
Please explain "is not working" with any details. It is easier to solve a problem than to guess, what the problem is.

Iniciar sesión para comentar.

Respuestas (1)

Yash
Yash el 30 de Sept. de 2023
Hi Yogeshwari,
I understand that you have two queries:
  1. The value of 'Y(5)' is not 1 through your code.
  2. You are interested in substituting the value of a in series and want to get series in terms of a.
For the first query, upon observation, I have found out that the value you have obtained is very close to 1. When pasting the same value in the MATLAB command window, it evaluates to 1. To obtain the desired result, you can use the "eval" function to evaluate the expression. You just need to make one change in your code as follows:
Y(i+1)=eval((gamma(a*(i-1)+1)/gamma((a*(i-1)+3/2))*(A-Y(i)+((2*B)/gamma(5/2)))));
You can refer to the MATLAB documentation of the "eval" function in the following link:
Regarding the second query, I suggest you to first change the name of the variable which stores the value of 'a' after solving 'M'. This will help to avoid confusion between the symbolic variable 'a' and the variable 'a' used in your code.
To substitute the value of 'a', you can use the "subs" function as shown below:
aval=solve(M) % find the value of a
series(x) = subs(series(x),a,aval)
The MATLAB documentation of the "subs" function is available at the below link:
You can further use the "eval" function if the expressions are complex. If the values involve large fractions, you can use the "vpa" function to apply variable-precision arithmetic.
Refer to the below link to get more details of the "vpa" function:
I hope this addresses your query.
Best Regards
Yash

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by