-2

i have the same problem as this discussion before,

[here] Can someone explain how to graph this sum in MATLAB using contourf?

however, for now i haven't deal with the contourf, because i have two other question that i have to answer.

I'm told to take N = 50, phi1 = 300, phi2 = 400, 0<=x<=1, and 0<=y<=1, and to let x and y be vectors of 100 equally spaced points, including the end points.

and heres the two question,

a) In a single figure (Figure 1), plot phi(x,y=1), phi(x,y=0.8), phi(x,y=0.5), and phi(x,y=0.1). Make sure the axes are labeled, and the curves are in distinct colors.

b) In a separate figure (Figure 2), plot phi(x,y=1), phi(x,y=0), phi(x=0,y), and phi(x=1,y) as subplots (i.e. 4 separate sub-figures). Make sure everything is labeled, and the greek letter phi appears as is in the yaxis label.

I have already answer for question a), however when i try to answer question b, i got the error. help?

clear all
close all
clc
clear

N=50;
phi1=300;
phi2=400;
%Phix10
%Phix08
%Phix05
%Phix01
x=linspace(0,1,100);
y=linspace(0,1,100);

y=1;
for k=1:100
   sum=0;
   tot=0;
    for n=1:N
        sum= sum + (2/n*pi)*((phi2-phi1)*((cos(n*pi)-1))/(exp(n*pi)-exp(-n*pi)))*((1-exp(-n*pi))*exp(n*pi*y)+(exp(n*pi)-1)*exp(-n*pi*y))*sin(n*pi*x(k));
    end
    Phi10(k)=phi1 - sum;
end

y=.8;
for k=1:100
   sum=0;
   tot=0;
    for n=1:N
        sum= sum + (2/n*pi)*((phi2-phi1)*((cos(n*pi)-1))/(exp(n*pi)-exp(-n*pi)))*((1-exp(-n*pi))*exp(n*pi*y)+(exp(n*pi)-1)*exp(-n*pi*y))*sin(n*pi*x(k));   
    end
    Phi08(k)=phi1 - sum;
end

y=.5;
for k=1:100
   sum=0;
   tot=0;
    for n=1:N
        sum=sum + (2/n*pi)*((phi2-phi1)*((cos(n*pi)-1))/(exp(n*pi)-exp(-n*pi)))*((1-exp(-n*pi))*exp(n*pi*y)+(exp(n*pi)-1)*exp(-n*pi*y))*sin(n*pi*x(k));
    end
    Phi05(k)=phi1 - sum;
end

y=.1;
for k=1:100
   sum=0;
   tot=0;
    for n=1:N
        sum=sum + (2/n*pi)*((phi2-phi1)*((cos(n*pi)-1))/(exp(n*pi)-exp(-n*pi)))*((1-exp(-n*pi))*exp(n*pi*y)+(exp(n*pi)-1)*exp(-n*pi*y))*sin(n*pi*x(k));
    end
    Phi01(k)=phi1 - sum;
end


figure(1);
plot(x,Phi10,'r');
hold on;
plot(x,Phi08,'g');
hold on;
plot(x,Phi05,'y');
hold on;
plot(x,Phi01);
hold on;
title('plotting 1');
xlabel('x');
ylabel('y');


%my answer for question b
y=1;
for k=1:100
   sum=0;
   tot=0;
    for n=1:N
        sum= sum + (2/n*pi)*((phi2-phi1)*((cos(n*pi)-1))/(exp(n*pi)-exp(-n*pi)))*((1-exp(-n*pi))*exp(n*pi*y)+(exp(n*pi)-1)*exp(-n*pi*y))*sin(n*pi*x(k));
    end
    PhiB1(k)=phi1 - sum;
end

y=0;
for k=1:100
   sum=0;
   tot=0;
    for n=1:N
        sum = sum + (2/n*pi)*((phi2-phi1)*((cos(n*pi)-1))/(exp(n*pi)-exp(-n*pi)))*((1-exp(-n*pi))*exp(n*pi*y)+(exp(n*pi)-1)*exp(-n*pi*y))*sin(n*pi*x(k));
    end
    PhiB2(k)=phi1 - sum;
end

x=0;
for k=1:100
    sum=0;
    for n=1:N

          % here's where i got the error
          sum = sum + (2/n*pi)*((phi2-phi1)*((cos(n*pi)-1))/(exp(n*pi)-exp(-n*pi)))*((1-exp(-n*pi))*exp(n*pi*y(k))+(exp(n*pi)-1)*exp(-n*pi*y(k)))*sin(n*pi*x);
    end
    PhiB3(k)=phi1 - sum;
end

% x=1;
% for k=1:100
%     sum=0;
%     for n=1:N
%         sum= sum + (2/n*pi)*((phi2-phi1)*((cos(n*pi)-1))/(exp(n*pi)-exp(-n*pi)))*((1-exp(-n*pi))*exp(n*pi*y)+(exp(n*pi)-1)*exp(-n*pi*y))*sin(n*pi*x);
%     end
%     PhiB4(k)=phi1 - sum;
% end

figure(2);
plot(x,PhiB1,'r');
hold on;
plot(x,PhiB2,'g');
hold on;
plot(x,PhiB3,'y');
hold on;
plot(x,PhiB4);
hold on;
Community
  • 1
  • 1
  • You define `y` with `linspace`, but then you set `y=1`. What's going on there? Also, Don't use `sum` as a variable. – chappjc Jan 23 '14 at 23:10
  • i was just trying to answer the question with the same way I answer part A. since it did works, however i have problem when they fixed the x and y can be change. – user3229895 Jan 23 '14 at 23:16
  • Huh? "i have a problem when they fixed the x and y can be change" Can you explain this a bit more? – chappjc Jan 23 '14 at 23:18
  • so if you see the question, it give condition where (x,y=1) and (x,y=0), i have no problem here, but i have problem when i try to do the (x=0,y) and (x=1,y). do you have any idea on how to do this? – user3229895 Jan 23 '14 at 23:21

1 Answers1

0

You have y(k) in the line where the error is, but y=0, and so it is not a vector. Replace the 2 occurrences of y(k) with y and see what happens.

David
  • 8,449
  • 1
  • 22
  • 32
  • i already tried that before, and it went so big, moreover, i think it makes my for loop is useless isn't it? if you look my answer for part a, i got it all correct, and then i just make necessary adjustment to answer part b, if you can see I have pretty similar answer for a and b. any idea? – user3229895 Jan 23 '14 at 23:18
  • Two comments. 1- You don't have `y(k)` anywhere else, only in the line giving you the error. 2- Yes I think your loops are useless, I think this could be done without all these loops. – David Jan 23 '14 at 23:19
  • i didn't think it answer the question though.. because i think part a and b is similar in some way, and i got the correct answer for part a, but not for part b, which is the problem is in x=1 and x=0 part. in part a i use x(k), so i am thinking for this part i switch it to y(k) – user3229895 Jan 23 '14 at 23:29
  • But `x` is a vector, and `y` is not. You define `y` as a vector, but you never use it before overwriting it as `y=1`, then `y=.8`, etc. so you never use it as a vector. Change the `y(k)` in that specific line to `y`, and your code will work. You have used just `y` everywhere else anyway, so why should this one part be different??? – David Jan 23 '14 at 23:32
  • well.. i am not sure about what you just said, but i did not get a good graph for the second one if i omit the k. so i am gonna keep trying for now. i am not sure for now. – user3229895 Jan 24 '14 at 00:47
  • actually.. i found the mistake, you are right. so i think i made mistake also for the first question.. – user3229895 Jan 24 '14 at 00:59