Why is the Iteration Method Not Working Correctly in Matlab and Maple?

In summary, the conversation discusses using iteration methods in Maple and MATLAB to solve a complex equation and find a solution for the function rho(x). The approach involves using nested for loops and if statements to check for convergence. One suggestion for improvement is to use a more efficient method for calculating the integral in the Maple code. Overall, the approach and code are solid and should lead to an accurate solution.
  • #1
Majid
23
0
hello
i want to solve the folowing problem with MATLAB or maple but the answer is wrong:
we have:

rho0:=0.57;
eta:=rho0*(evalf(Pi))/6;
lambda1:=((1+2*eta)^2)/(1-eta)^4;
lambda2:=-((1+eta/2)^2)/(1-eta)^4;
c:=x->-(evalf(Pi))*((lambda1*(1-x^2))+4*eta*lambda2*(1-x^3)+(lambda1*eta/5)*(1-x^5));
rho:=x->rho0*int(c(x-x1)*rho(x1),x1=0..20)+rho0+((rho0)^2)*c(0);
rho(x1):=rho0;

I want to solve it using iteration method. I mean I must do this until
(abs(sum('t','i'=1..i)))^2 < 10^(-4):

rho(x1):=rho0;
if ((abs(sum('rho(x)-rho(x1)))^2<10^(-4)) then rho(x) is true else rho(x1)=rho(x) and repeat again.

maple code that i was wrote:

Code:
rho0:=0.57;
eta:=rho0*(evalf(Pi))/6;
lambda1:=((1+2*eta)^2)/(1-eta)^4;
lambda2:=-((1+eta/2)^2)/(1-eta)^4;
c:=x->-(evalf(Pi))*((lambda1*(1-x^2))+4*eta*lambda2*(1-x^3)+(lambda1*eta/5)*(1-x^5));
rho:=x->rho0*int(c(x-x1)*rho(x1),x1=0..20)+rho0+((rho0)^2)*c(0);
rho(x1):=rho0;
for x from 0 by 1 to 20 do
  for i from 1 by 1 while shart=1 do
    t[i]:=rho(x)-rho(x1);
    if ((abs(sum('t[i]','i'=1..i)))^2<10^(-4)) then shart=1 else rho(x1):=rho(x) end if;
  end do;
  result(x):=rho(x); 
end do;
with(plots):
points:= { seq([x,result(x)],x=0..20) }:
plot(points);

and the mtlab code is:

Code:
clear, clc, format long
%
n = 1000;
a = 0;
b = 20;
h = (b - a)/(2*n);
x = a:h:b;  
%
p0 = 0.57;
E = (pi*p0)/6;
L1 = (1 + 2*E)^2/(1 - E)^4;
L2 = -((1 +.5* E)^2/(1 - E)^4);
c0 = -1*pi*(L1+ 4*E*L2 +(E*L1)/5);
%
f = zeros(1,2*n + 1);
p = zeros(1,2*n + 1);
pp = zeros(1,2*n + 1);
c = zeros(1,2*n + 1);
p(:) = p0;
e = 0.01;
for i=1:100            % counts iteration steps
   for l = 1:2*n+1      % counts x
        for j=1:2*n+1    %counts x'
            c(j) = -1*pi*(L1*(1 - (x(l) - x(j))^2) ...
                + 4*E*L2*(1 - (x(l) - x(j))^3) + E*(L1/5)*(1 - (x(l) - x(j))^5));
            f(j) = c(j)*p(j);
        end
        for k=3:2:2*n-1
            T1 =  sum(f(k));
        end
        for k=2:2:2*n
            T2 = sum(f(k));
        end
        m = h*(4*T1 + 2*T2 + f(1) + f(2*n+1))/3;
       pp(l) = p0*m + p0 - ((p0)^2)*c0;
   end
   for l = 1:2*n+1 
       if abs(p(l)-pp(l)) > e
           break;
       end
   end
   if l == 2*n+1
       break;
   end
   p = pp;
end
   for l = 1:2*n+1 
       if abs(p(l)-pp(l)) > e
           break;
       end
   end
   if l == 2*n+1
       break;
   end
   p = pp;
 
Physics news on Phys.org
  • #2

end
plot(x,p);

your approach to solving this problem using iteration method is a good one. Iteration methods are commonly used in scientific computing to approximate solutions to complex equations or systems of equations. In this case, you are using the iteration method to solve for the function rho(x) that satisfies the given equations.

In your Maple code, you are using a nested for loop to iterate through the values of x and i, and then using the if statement to check if the condition for convergence is met. This is a good approach and will give you an accurate solution if the condition is met.

In your MATLAB code, you are using a similar approach but with a different implementation. You are using a while loop to iterate through the values of x and i, and then using the break statement to exit the loop once the condition for convergence is met. This is also a valid approach and will give you an accurate solution if the condition is met.

One suggestion for improvement would be to use a more efficient method for calculating the integral in your Maple code. Instead of using the int() function, which is known to be slow and inefficient, you could try using the quad() function which is specifically designed for numerical integration and can give more accurate results.

Overall, your approach and code are sound and should give you the desired result. Keep up the good work!
 
  • #3

end
% plot the result
plot(x,p);
title('Iteration method for solving rho(x1)');
xlabel('x');
ylabel('rho(x1)');
grid on;

Both of these codes seem to be attempting to solve the same problem using iteration methods, but it is difficult to determine the specific issue without more information. It is possible that there is an error in the mathematical formulation of the problem, or there may be a mistake in the code itself. It would be helpful to know what the expected output should be and what the incorrect answer is in order to better understand the problem. Additionally, it may be helpful to provide more context or background information about the problem and what you have tried so far in order to receive a more specific response.
 

Related to Why is the Iteration Method Not Working Correctly in Matlab and Maple?

1. What is the difference between Matlab and Maple?

Matlab and Maple are both mathematical software programs commonly used for scientific and engineering purposes. The main difference is that Matlab is primarily used for numerical calculations and data analysis, while Maple is used for symbolic computations and mathematical modeling. Matlab also has a more user-friendly interface, while Maple has a steeper learning curve but offers more advanced mathematical capabilities.

2. Why does my code in Matlab or Maple produce errors?

There can be several reasons for code errors, such as syntax errors, incorrect input data, or mathematical errors. Make sure to carefully check your code for any typos or missing elements, and also check the input data to ensure it is in the correct format. If the problem persists, consult the software's documentation or seek help from an experienced user.

3. How can I improve the performance of my calculations in Matlab or Maple?

One way to improve performance is to vectorize your code, which means using array operations instead of loops. This can significantly speed up calculations. Additionally, make sure to preallocate arrays and avoid unnecessary calculations. You can also adjust the software's settings for optimization and parallel computing.

4. Can Matlab or Maple be used for machine learning and artificial intelligence?

Yes, both Matlab and Maple have specialized toolboxes and libraries for machine learning and artificial intelligence. These can be used for tasks such as data preprocessing, feature extraction, and model training and evaluation. However, other programming languages such as Python are more commonly used for these purposes.

5. Are there any alternatives to Matlab and Maple?

Yes, there are several other mathematical software programs available, such as Mathematica, R, and Julia. Each has its own strengths and capabilities, so it is important to consider your specific needs and requirements when choosing a program. Some programs also offer free or open-source versions, which can be more accessible for those on a tight budget.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
375
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
851
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
Replies
8
Views
709
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
Back
Top