How to make a function output a matrix

조회 수: 30 (최근 30일)
Petch Anuwutthinawin
Petch Anuwutthinawin 2021년 6월 17일
답변: SALAH ALRABEEI 2021년 6월 17일
I am trying to make a function that takes in multiple variables and outputs a matrix of values. The inputs are supposed to plug into the equation V, and output a matrix. My problem is that this outputs a vector instead of a matrix. How do I make it output a matrix?
function T = prob3_5(x,y,N,L,W,T1,T2)
V=zeros(1,length(x));
for n=1:2:N
for i=1:length(x)
V(i)=V(i)+(2/pi)*(2/n)*sin(n*pi*x(i)/L).*sinh(n*pi*y(i)/L)/sinh(n*pi*W/L);
end
T=(T2-T1)*V+T1;
end
  댓글 수: 3
SALAH ALRABEEI
SALAH ALRABEEI 2021년 6월 17일
% This is a Fourier series why do you want a matrix, it is just an array
% because you just summing up the solution at each point of x and y.
% The output is vector because T is V and V is defined as a vector V=zeros(1,length(x));
Petch Anuwutthinawin
Petch Anuwutthinawin 2021년 6월 17일
the answer should have the same number of columns as elements in X, and the same number of rows as elements in Y.

댓글을 달려면 로그인하십시오.

채택된 답변

SALAH ALRABEEI
SALAH ALRABEEI 2021년 6월 17일
I think you need only one loop that accomulate the series. I am assuming x and y are sent to the function as vectors, then we creat grid out of them
T = prob3_5(x,y,N,L,W,T1,T2)
[x,y]=meshgrid(x,y);
V=zeros(size(x));
for n=1:2:N
V=V+(2/pi)*(2/n)*sin(n*pi*x/L).*sinh(n*pi*y/L)./sinh(n*pi*W/L);
T=(T2-T1)*V+T1;
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by