楼主: hrbatanu
1646 10

[问答] 一道入门matlab问题 [推广有奖]

  • 17关注
  • 4粉丝

讲师

41%

还不是VIP/贵宾

-

威望
0
论坛币
199 个
通用积分
0.2400
学术水平
2 点
热心指数
6 点
信用等级
1 点
经验
4810 点
帖子
265
精华
0
在线时间
679 小时
注册时间
2011-6-23
最后登录
2022-6-4

100论坛币
小弟报了一门课用matlab,虽然题目简单,但是毫无编程和matlab基础。希望大家指教 题目
以上是题目,接下来是我写的code,不在学校手里没有matlab,暂时没run:

%%initial the values
a=2;

while (s > 0.0001)
    xold=x;
    x=(x+a/x)/2;
    s=abs((x-xold)/x);
    if(s<0.0001)
        break
    end
end
上面的哪些地方要改呢?
还有,如何reture both the result and error? 有个return的code?
display the result as a imaginary number? matlab不是自动用i吗?

谢谢


最佳答案

24578901 查看完整内容

首先介绍一下divide and average method Let A be the number whose square root is wanted. Determine in some way a first guess g of the square root of A. The remaining part of the method is to repeat the following step: Divide A by the current guess g and then average the quotient with g. The average is the new guess.(首先是要对A的平方根做一个估计,我这用x=a/2估计) Repeat until you have calcu ...
关键词:matlab问题 MATLAB atlab matla Mat matlab

本帖被以下文库推荐

沙发
24578901 在职认证  发表于 2013-6-29 11:58:37 |只看作者 |坛友微信交流群
首先介绍一下divide and average method
Let A be the number whose square root is wanted.  Determine in some way a first guess g of the square root of A.  The remaining part of the method is to repeat the following step: Divide A by the current guess g and then average the quotient with g.  The average is the new guess.(首先是要对A的平方根做一个估计,我这用x=a/2估计)

Repeat until you have calculated the square root to desired accuracy.  Roughly, the number of decimal places doubles with each repetition.

上传了两幅一样的图,竟然删不了,Sorry

流程.jpg (21.86 KB)

流程.jpg

新建 Microsoft Visio 绘图.jpg (1.42 KB)

新建 Microsoft Visio 绘图.jpg

使用道具

藤椅
24578901 在职认证  发表于 2013-6-29 12:26:07 |只看作者 |坛友微信交流群
题目的意思是,根据变换正数a,以获得合理的误差求平方根。
要想有返回值,M文件可以这样写function [result,error]=myfun(x)

使用道具

板凳
hrbatanu 发表于 2013-6-29 12:52:01 |只看作者 |坛友微信交流群
24578901 发表于 2013-6-29 12:26
题目的意思是,根据变换正数a,以获得合理的误差求平方根。
要想有返回值,M文件可以这样写function [resu ...
恩,要求a的平方根,先代入2,循环找到一个x给出error在可接受范围内,从而知道a。但是为什么要求make sure it can evaluate the square of number that are equal to and less than zero? 负数的话,不是自动用复数吗?

使用道具

报纸
24578901 在职认证  发表于 2013-6-29 14:55:48 |只看作者 |坛友微信交流群
hrbatanu 发表于 2013-6-29 12:52
恩,要求a的平方根,先代入2,循环找到一个x给出error在可接受范围内,从而知道a。但是为什么要求make su ...
divide and average method是逼近任意正数a(any positive number a)的平方根的一个古老方法,如果要a系小于或等于0,就不能用divide and average method了

使用道具

地板
hrbatanu 发表于 2013-6-30 12:07:48 |只看作者 |坛友微信交流群
24578901 发表于 2013-6-29 16:07
首先介绍一下divide and average method
Let A be the number whose square root is wanted.  Determine i ...
恩,第二个图挂了。第一幅图逻辑很清楚,但是我自己还是写不对,我觉得我刚开始学应该多看,多模仿,自己瞎写浪费时间,请问你能帮我直接写出来看看吗?

使用道具

7
flysyq 在职认证  发表于 2013-6-30 14:10:12 |只看作者 |坛友微信交流群
function [result,error,iter]=sqrt_test(x,a)
s=10^-4;
ju=1;
t=0;
i=0;
er = 0;
if(a<0)
        ju = 0;
        a=-a;
end

while(t==0)
        xnew = (x+a/x)/2;
        er = abs((xnew-x)/xnew);
        x = xnew;
        i=i+1;
        if(er<s)
                t = 1;
                break;
        end
end
if(j==1)
result = x;
else
result = strcat(num2str(x),"i");
end
error = er;
iter = i;
end

使用道具

8
hrbatanu 发表于 2013-7-1 09:09:07 |只看作者 |坛友微信交流群
flysyq 发表于 2013-6-30 14:10
function [result,error,iter]=sqrt_test(x,a)
s=10^-4;
ju=1;
>> sqrt_test
Error: File: sqrt_test.m Line: 26 Column: 28
The input character is not valid in MATLAB statements or expressions.

使用道具

9
flysyq 在职认证  发表于 2013-7-1 14:57:02 |只看作者 |坛友微信交流群
function [result,error,iter]=sqrt_test(x,a)
s=10^-4;
ju=1;
t=0;
i=0;
er = 0;
if(a<0)
        ju = 0;
        a=-a;
end

while(t==0)
        xnew = (x+a/x)/2;
        er = abs((xnew-x)/xnew);
        x = xnew;
        i=i+1;
        if(er<s)
                t = 1;
                break;
        end
end
if(ju==1)
result = x;
else
result = strcat(num2str(x),'i');
end
error = er;
iter = i;
end

使用道具

10
flysyq 在职认证  发表于 2013-7-1 14:58:46 |只看作者 |坛友微信交流群
if(ju=1)->if(ju==1)
result = strcat(num2str(x),"i"); ->result = strcat(num2str(x),'i');

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-6-16 14:26