Plot is zeroed out, how to fix this?

Hello,
I wrote a code that should give set values and plot them. For some reason i keep getting zero values. Any thoughts?
Now i am faced with a different problem. My plot comes out zeroed and i have no clue why. Any thoughts?
clc;
clear;
N=25;
% --Grid--
gridX = linspace(0,1,N);
gridT = linspace(0,0.5,N);
[x,t] = meshgrid(gridX,gridT);
A = linspace(0,1,N);
u = zeros(N,N);
xint = linspace(0,1,N);
%--Computation of An Terms--
n=1;
while n <= N;
I(n) = -(4*sin((pi*n)/2))/(pi*(n^2 - 4));
A(n) = A(n) + I(n);
n = n + 1;
end
while n <= N
exponent = -(n.^2)*(pi.^2)*(t); % n replaces lamda
T = exp(exponent);
X = A(n).*sin(n.*pi.*x);
total = T*X;
u(n) = u(n) + total; %--Sum of all solutions--
n = n + 1;
end
%--Plot--
figure;
surfc(x,t,u)
title('Project 2')
xlabel('X')
ylabel('Time')

回答 (1 件)

Mischa Kim
Mischa Kim 2014 年 2 月 11 日

0 投票

Mike, after initializing u to zero you enter the first loop that increments n all the way to N. Which is why you never enter the second loop that is supposed to update u.

4 件のコメント

Mike
Mike 2014 年 2 月 11 日
Well how would i go about changing that?
Lets just say i am not so good that this program at the moment.
Do i have to reassign an n value before before entering the second loop again?
Mischa Kim
Mischa Kim 2014 年 2 月 11 日
Sorry about that. Yes, simply set n=1 right before entering the second loop.
Alternatively, you could try to combine the two loops. Any reason you kept them separate?
Mike
Mike 2014 年 2 月 11 日
My lack of knowledge on loops is why they are seperate. I figured combining them would be better but i kept having issues when doing that.
Mischa Kim
Mischa Kim 2014 年 2 月 11 日
Just looked at your code. Since I do not know what exactly you need to compute here are some thoughts:
  • Change the corresponding command to: u = u + total. That way at least the dimensions work out.
  • You are generating some huge numbers along the way so there are probably issues with some of the equations.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2014 年 2 月 11 日

コメント済み:

2014 年 2 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by