index exceed matrix dimensions

1.1k Views Asked by At

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;
1

There are 1 best solutions below

6
On

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.