Creating a function with equation as an input
    조회 수: 12 (최근 30일)
  
       이전 댓글 표시
    
HI, I am trying to create a function,with an equationas an input so that the equation runs within 4 loops while the values of the equation change with each iteration.
 I want to use a function as the process nedds to be repeated for different equations multiiple times.
However matlab does not recognise variable p, i understand it hasnt been defined but im not sure how to input the equation so that it can be read and suitibly substituted in the equation.
I appreciate any assistance thank you
for Sigmainternal = 1:1
   %sigma11 refers to 11:==> 1_: First U equation _1: fisrt term in equation.
   sigma11 = stepPQ(N,  ((p*p1/(p+p1-1))*((1-(-1)^(q+q1+1))/(q+q1+1))) );
end
function [sigma] = stepPQ(N,eqn)
    sigma =zeros((N+1)^2,(N+1)^2);
    for p=0 : N
        for q = 0:N
            for p1 = 0:N
                for q1 = 0:N
                    temp = (eqn);
                    if isnan(temp) %check if divide by 0
                        temp = 0;
                    end
                    row = p*(N+1)+(q+1);
                    col = p1*(N+1)+(q1+1);
                    sigma(row,col) = temp ;
                end
            end
        end
    end
  sigma = sigma + transpose(sigma);              
end
댓글 수: 0
채택된 답변
  Alan Stevens
      
      
 2021년 4월 7일
        Like this:
   %sigma11 refers to 11:==> 1_: First U equation _1: fisrt term in equation.
  N = 3; % arbitrary value
  sigma11 = @(p,q,p1,q1) p*p1/(p+p1-1)*(1-(-1).^(q+q1+1))/(q+q1+1); % create as a function
  sigma = stepPQ(N, sigma11);  % call stepPQ with N and the function sigma11
  disp(sigma) 
function [sigma] = stepPQ(N,eqn)
    sigma =zeros((N+1)^2,(N+1)^2);
    for p=0 : N
        for q = 0:N
            for p1 = 0:N
                for q1 = 0:N
                    temp = eqn(p,q,p1,q1);  %  You have to call the equation with arguments
                    if isnan(temp) %check if divide by 0
                        temp = 0;
                    end
                    row = p*(N+1)+(q+1);
                    col = p1*(N+1)+(q1+1);
                    sigma(row,col) = temp ;
                end
            end
        end
    end
  sigma = sigma + transpose(sigma);              
end
Note that sigma11 isnt an equation, it's a function.
추가 답변 (1개)
  Julius Muschaweck
      
 2021년 4월 7일
        
      편집: Julius Muschaweck
      
 2021년 4월 7일
  
      Your "equation" seems to be a function depending on p, p1, q, q1. 
I would use an anonymous function to model this behavior. An anonymous function is an object you can feed into another function and evaluate within that other function. 
(You also need to define N to make this example run. With N = 3, sigma11 becomes a 4x4 matrix)
for Sigmainternal = 1:1
   %sigma11 refers to 11:==> 1_: First U equation _1: fisrt term in equation.
   % define N
   N = 3;
   % define equation
   eqn = @(p, p1, q, q1) ((p*p1/(p+p1-1))*((1-(-1)^(q+q1+1))/(q+q1+1)));
   sigma11 = stepPQ(N,  eqn );
end
function [sigma] = stepPQ(N,eqn)
    sigma =zeros((N+1)^2,(N+1)^2);
    for p=0 : N
        for q = 0:N
            for p1 = 0:N
                for q1 = 0:N
                    temp = eqn(p,p1,q,q1);
                    if isnan(temp) %check if divide by 0
                        temp = 0;
                    end
                    row = p*(N+1)+(q+1);
                    col = p1*(N+1)+(q1+1);
                    sigma(row,col) = temp ;
                end
            end
        end
    end
  sigma = sigma + transpose(sigma);              
end
댓글 수: 0
참고 항목
카테고리
				Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


