 help me
 help me CODE error correction
    4 次查看(过去 30 天)
  
       显示 更早的评论
    
HI all,
would somebody please help me find out what should i do to plot and correct this script?
clear
clc;
B=1000; L=2000;Df=200; Sgama=.8;
nsamples=3000;
rmem = zeros(3,nsamples);
qult = zeros(1,nsamples);
for K=1:nsamples
  while true
  C = normrnd(620,147.64);
  if C<=450 || C >= 800 || ismember(C,rmem(1,1:K-1)); continue; end
   gama = normrnd(1.96,0.02);
  if gama <= 1.92 || gama >= 1.98 || ismember(gama,rmem(2,1:K-1)); continue; end
   fi = normrnd(3.76,1.1034);
  if fi <= 2.7 || fi >= 16.3 || ismember(fi,rmem(3,1:K-1)); continue; end
  rmem(:,K) = [C; gama; fi];
   break
  end
   q=107.25+(100*fi);
   dq=1+(.4*tan(fi*pi/180)*(1-sin(fi*pi/180)^2));
   Sq=1+(.5*sin(fi*pi/180));
   Nq = tan((pi/4)+(pi*fi/360)) * tan((pi/4)+(pi*fi/360)) * exp(pi*tan(fi*pi/180));
   Nc = (Nq-1)*cot(fi*pi/180);
   Sc=1+(.5* Nq / Nc);
    Ngama = 2*(Nq+1)*tan(fi*pi/180);
    qult(K)=(1.08*C*Nc*Sc)+(q*Nq*Sq*dq)+(429*Ngama);
  end
surf( C(1,1:nsamples), fi(1,1:nsamples), qult );
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
ERROR :
??? Index exceeds matrix dimensions.
Error in ==> graphQ at 27 surf( C(1,1:nsamples), fi(1,1:nsamples), qult );
回答(3 个)
  UJJWAL
      
 2011-9-25
        Hi ,
I don't see any reason as to why thic code should not give an error. C is not a vector. It is a single number(or let us say C is a vector of dimension 1 by 1. In the surf function you are trying to access C(1,1:nsamples) which obviously goes beyond the length of C . So an error must occour.
As far as correction is concerned, it is difficult unless you tell the nature of this code and what it is that you want to do. I defined a vector C1 and stored the individual values of C in it. I did the same for fi . However in that case surf would not work as it will require a 2D matrix in the form of qult to work. I believe that you want to plot qult for corresponding values of C and fi. However for that u need to use Plot3. I used it but I got something weird. So unless the nature of the code is not made clear it is extremely difficult to comment on the nature of the solution.
Happy to Help
2 个评论
  Walter Roberson
      
      
 2011-9-25
				UUJWAL is correct: with that flow of code, you will need to store each accepted C and fi value. Just before your q= assignment would seem to be an appropriate location.
  Vikash Anand
 2021-12-13
        clc;
clear all;
close all;
n=7;k=4;
num_bit=10000;
genpoly=cyclpoly(n,k,'max');
SNRdB=0:10;
SNR=10.^(SNRdB/10);
for i=1:length(SNR)
 msg = randi(num_bit,k,[0,1]);
 code=encode(msg,n,k,'cyclic/binary',genpoly);
 [row, column]=size(code);
 codevec=reshape(code.',1,row*column);
 noise=awgn(codevec,SNRdB(i));
 y=codevec+noise;
 error=0;
 for j=1:length(y)
 if (y(j)>1&&codevec(j)==0)||(y(j)<0&&codevec(j)==1) 
 error=error+1;
 end
 end
 error=error/num_bit; 
m(i)=error;
end
y(y>0)=1;
 y(y<0)=0;
 decode_y=decode(y,n,k,'cyclic/binary',genpoly);
 decmsg=reshape(decode_y,num_bit,k);
 semilogy(SNRdB,m,'r','linewidth',2),grid on;
title(' Bit Error Rate verses SNR for Cyclic Code');
xlabel(' SNR(dB)');
ylabel('BER');
1 个评论
  Walter Roberson
      
      
 2024-11-10
				I do not understand how this is an answer to the question that was originally asked?
  RAJESH
 2024-9-25
        
      编辑:Walter Roberson
      
      
 2024-9-25
  
      clc; clear all; close all;
% Input data points
x = [0 3 4 5 6];    % x-coordinates
y = [3 6 1 7 5];    % y-coordinates
% Number of intervals
N = length(x);
% Spline parameters (can be tuned for smoothness)
alpha = [0.5 0.8 0.7 0.6 0.9];  % Scale control parameters
r = 3 * ones(1, N);              % Shape parameters
intervals = [0 5; 3 5; 0 4; 3 4; 5 6];  % Define intervals
% Derivative values (arbitrary initialization for now)
d = [5.5 -3.5 0.5 2.0 -6.0];
d1 = [3.5 -1.5 5.5 2.5 1.5];
% Prepare X1 and Y1 matrices for the initial interpolation data
X1 = zeros(4, 4);  % Initialize X1 with zeros
x1 = [x(1) x(2) x(3) x(4)];
x2 = [x(2) x(3) x(4)];
x3 = [x(1) x(2) x(3)];
x4 = [x(2) x(3)];
t = [length(x1) length(x2) length(x3) length(x4)];
X1(1, 1:t(1)) = x1;
X1(2, 1:t(2)) = x2;
X1(3, 1:t(3)) = x3;
X1(4, 1:t(4)) = x4;
Y1 = zeros(4, 4);  % Initialize Y1 with zeros
y1 = [y(1) y(2) y(3) y(4)];
y2 = [y(2) y(3) y(4)];
y3 = [y(1) y(2) y(3)];
y4 = [y(2) y(3)];
t = [length(y1) length(y2) length(y3) length(y4)];
Y1(1, 1:t(1)) = y1;
Y1(2, 1:t(2)) = y2;
Y1(3, 1:t(3)) = y3;
Y1(4, 1:t(4)) = y4;
% Loop to compute parameters
a = zeros(1, 4);  % Initialize arrays for a(i) and b(i)
b = zeros(1, 4);
for i = 1:4
    % Compute a(i) and b(i)
    a(i) = (x(i+1) - x(i)) / (X1(i, t(i)) - X1(i, 1));
    b(i) = (X1(i, t(i)) * x(i) - X1(i, 1) * x(i+1)) / (X1(i, t(i)) - X1(i, 1));
end
% Main loop for calculating terms and interpolation
iter = 1;
L = []; L1 = [];
for i = 1:4
    rho = (x(i) - X1(i, 1)) / (X1(i, t(i)) - X1(i, 1));  % Calculate rho
    % Calculate terms
    term1 = (y(i) - alpha(i) * Y1(i, 1)) * (1 - (x(i) - X1(i, 1)) / (X1(i, t(i)) - X1(i, 1))).^3;
    term2 = (y(i+1) - alpha(i) * Y1(i, t(i))) * ((x(i) - X1(i, 1)) / (X1(i, t(i)) - X1(i, 1))).^3;
    % Prevent accessing out of bounds
    if i < N
        term3 = (r(i) * (y(i) - alpha(i) * Y1(i, 1)) + (x(i+1) - x(i)) * d(i) - alpha(i) * d1(i, 1) * (X1(i, t(i)) - X1(i, 1))) * ...
            (1 - (x(i) - X1(i, 1)) / (X1(i, t(i)) - X1(i, 1))).^2 * (x(i) - X1(i, 1)) / (X1(i, t(i)) - X1(i, 1));
        term4 = (r(i) * (y(i+1) - alpha(i) * Y1(i, t(i))) - ((x(i+1) - x(i)) * d(i+1)) + alpha(i) * d1(i, t(i)) * (x1(i, t(i)) - x1(i, 1))) * ...
            (1 - (x(i) - X1(i, 1)) / (X1(i, t(i)) - X1(i, 1))) * ((x(i) - X1(i, 1)) / (X1(i, t(i)) - X1(i, 1))).^2;
    else
        term3 = 0;  % Handle boundary condition
        term4 = 0;  % Handle boundary condition
    end
    % Calculate numerator and denominator
    numerator = term1 + term2 + term3 + term4;
    denominator = 1 + (r(i) - 3) * (1 - (x(i) - X1(i, 1)) / (X1(i, t(i)) - X1(i, 1))) * ((x(i) - X1(i, 1)) / (X1(i, t(i)) - X1(i, 1)));
    % Calculate q(i)
    q(i) = numerator / denominator;
end
% Plot the result
plot(x, y, '.k', 'markersize', 20);  % Original data points
hold on;
plot(X1(1,:), Y1(1,:), 'b-');  % Interpolated fractal curve
xlabel('x');
ylabel('y');
title('Recurrent Rational Fractal Cubic Spline');
grid on;
Index in position 1 exceeds array bounds. Index must not exceed 1.
3 个评论
  Walter Roberson
      
      
 2024-11-10
				You have edges(1:end-1) plotted against counts, but edges(1:end-1) is one shorter than counts is because edges and counts are the same size.
  Walter Roberson
      
      
 2024-11-10
				d1 = [3.5 -1.5 5.5 2.5 1.5];
d1 is 1 x 5
        term3 = (r(i) * (y(i) - alpha(i) * Y1(i, 1)) + (x(i+1) - x(i)) * d(i) - alpha(i) * d1(i, 1) * (X1(i, t(i)) - X1(i, 1))) * ...
You access d1(i,1). When i becomes 2 that would be d1(2,1). However, there is no d1(2,1), only d1(1,2)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!






