![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/247510/image.png)
Help with plotting 3d function
조회 수: 1 (최근 30일)
이전 댓글 표시
![Skärmavbild 2019-11-09 kl. 17.33.06.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/247509/Sk%C3%A4rmavbild%202019-11-09%20kl.%2017.33.06.png)
Hey!
I need help with exercise 2.b. This is my script that i have written
%% Exercise 2.b
clear
clf
clc
k=1;
L=6;
T=10
x=linspace(0,L,100); t=linspace(0,T,100);
for i=1 : length(x)
for j=1 : length(t)
u(i,j)=(8/(pi^2)).*(1./(n.^2)).*sin(n.*pi/2).*cos(n.*pi.*t(j)/6).*sin(n.*pi.*x(i)/6);
end
end
surf(t,x,u)
xlabel('t'); ylabel('x')
The problem that i need help with is how to put in the value of n. In the exercise it says that i should put n between 1->100 but i dont know how to do it....
Could someone pleas help me...?
댓글 수: 1
Star Strider
2019년 11월 9일
Note that n is the index with respect to
, and your code does not appear to be doing that summation.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/247510/image.png)
답변 (1개)
Praveen Iyyappan Valsala
2019년 11월 9일
Most intuitive way is to add another loop to sum up function outputs of all n.
for i=1 : length(x)
for j=1 : length(t)
for n=1:100
u(i,j)=u(i,j)+(8/(pi^2)).*(1./(n.^2)).*sin(n.*pi/2).*cos(n.*pi.*t(j)/6).*sin(n.*pi.*x(i)/6);
end
end
end
Little more efficient way.
n=1:100;
for i=1 : length(x)
for j=1 : length(t)
u(i,j)=(8/(pi^2)).*sum((1./(n.^2)).*sin(n.*pi/2).*cos(n.*pi.*t(j)/6).*sin(n.*pi.*x(i)/6));
end
end
if you want even more cool code. learn about meshgrid function.
참고 항목
카테고리
Help Center 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!