[問題]Error using ==> mpower 這個錯誤如何解?已回收

看板MATLAB作者 (Ryan)時間17年前 (2008/12/07 22:59), 編輯推噓1(109)
留言10則, 3人參與, 最新討論串1/1
這是 應用數值方法,王晉中譯 p12-20 頁的範例 x=[10 20 30 40 50 60 70 80]; y=[25 70 380 550 610 1220 830 1450]; linregr(x,y) %--------------------------------------------------- function [a,r2] = linregr(x, y) n = length(x); if length(y) ~= n, error('X and y must be same length'); end x = x(:); y = y(:); %convert to column vectors sx = sum(x); sy = sum(y); sx2 = sum(x.*x); sxy = sum(x.*y); sy2=sum(y.*y); a(1) = (n*sxy-x*sy)/(n*sx2-x^2); a(2) = sy/n-(1)*sx/n; r2 = ((n*sxy-x*sy)/sqrt(n*sx2-x^2)/sqrt(n*sy2-y^2))^2 % create plot of data and best fit line xp = linspace(min(x), max(x),2); yp = a(1)*xp+a(2); plot(x,y,'o',xp,yp) grid on --------------------------------------- 這個 M 檔執行時會出現以下錯誤 ??? Error using ==> mpower Matrix must be square. Error in ==> linregr at 12 a(1) = (n*sxy-x*sy)/(n*sx2-x^2); Error in ==> virus_test3 at 5 linregr(x,y) 使用 matlab 的經驗實在不足,爬了文,也試著去 debug, 找不出錯誤之處, 請有經驗的大大幫忙看看.... -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 122.121.181.181 ※ 編輯: ahph 來自: 122.121.181.181 (12/07 23:01) ※ 編輯: ahph 來自: 122.121.181.181 (12/07 23:02)

12/07 23:06, , 1F
.^ instead of ^
12/07 23:06, 1F

12/07 23:11, , 2F
我把 ^ 全改成 .^
12/07 23:11, 2F

12/07 23:12, , 3F
結果出現 ??? In an assignment A(I) = B, the number of
12/07 23:12, 3F

12/07 23:12, , 4F
and I must be the same.
12/07 23:12, 4F

12/07 23:42, , 5F
a(1)=(n*sxy-sx*sy)/(n*sx2-sx^2);
12/07 23:42, 5F

12/07 23:43, , 6F
a(2)=sy/n-a(1)*sx/n;
12/07 23:43, 6F

12/07 23:47, , 7F
r2=
12/07 23:47, 7F

12/07 23:49, , 8F
((n*sxy-sx*sy)/sqrt(n*sx2-sx^2)/sqrt(n*sy2-sy^2))
12/07 23:49, 8F

12/07 23:52, , 9F
上面那行整個在平方.中文本有漏打s喔.原文書是正確的
12/07 23:52, 9F

12/08 00:15, , 10F
感謝BlueRabbitLa大大指教,可以用了 ^_^
12/08 00:15, 10F
文章代碼(AID): #19E-IvER (MATLAB)
文章代碼(AID): #19E-IvER (MATLAB)