1.求方程 2 x 5 − 3 x 3 + 71 x 2 − 9 x + 13 = 0 2x^5-3x^3+71x^2-9x+13=0 2x5−3x3+71x2−9x+13=0 的全部根
>> p=[2 0 -3 71 -9 13];
>> pkg load symbolic
>> poly2sym(p)
Symbolic pkg v3.0.0: Python communication link active, SymPy v1.10.1.
ans = (sym)
     5      3       2
  2*x  - 3*x  + 71*x  - 9*x + 13
>> roots(p)
ans =
  -3.4914 +      0i
   1.6863 + 2.6947i
   1.6863 - 2.6947i
   0.0594 + 0.4251i
   0.0594 - 0.4251i
2.有一分数序列∶2/1,3/2,5/3,8/5,13/8,21/13…采用循环迭代求出这个数列的前n项之和。要求编写M函数∶function y=myFun(varargin)。函数要求能够自动判断输入输出参数的个数,并能够进行参数的判断∶
1)如果用户没有输入参数,默认n=20
2)若用户输入的参数大于1个,则取第一个参数信息,提示用户舍弃后面的参数。
3)如果用户没有输出参数,则在函数中用printf命令直接打印结果。
4)如果用户有一个输出参数,则将结果返回给输出参数,不用printf打印结果。
%方法一
function y=myFun(a,b)
   sum=t=0 ;
   fz=2;
   fm=1;
   i=1;
  if nargin==0
    n = 20;
  elseif nargin>=1
    n=a;
  end
  while i<=n
      sum=sum+fz/fm;
      t=fz;
      fz=fm+fz;
      fm=t;
      i++;
   end
  if nargout==0
    printf('%d',sum)
   elseif nargout==1
    y=sum
   end

%方法二
function varargout=myfun(varargin)
  a=[2 3];
  b=[1 2];
  s=0;
  if nargin==0
    n=20;
  else
    n=varargin{1};
  end
  for i=1:n
    if(i>=3)
      a(i)=a(i-1)+a(i-2);
      b(i)=b(i-1)+b(i-2);
     end
     s=s+a(i)/b(i);
  end
  if nargout==0
    fprintf("s=%f\n",s);
  else
    varargout{1}=s;
  end
end
3、写一个函数,实现分段表达式:
 
     
      
       
        
         f
        
        
         (
        
        
         x
        
        
         )
        
        
         =
        
        
         
          {
         
         
          
           
            
             
              
               0.5
              
              
               x
              
             
            
           
           
            
             
              
               x
              
              
               ≤
              
              
               2
              
             
            
           
          
          
           
            
             
              
               1.5
              
              
               −
              
              
               0.25
              
              
               x
              
             
            
           
           
            
             
              
               2
              
              
               <
              
              
               x
              
              
               <
              
              
               6
              
             
            
           
          
          
           
            
             
              0.5
             
            
           
           
            
             
              
               x
              
              
               ≥
              
              
               6
              
             
            
           
          
         
        
       
       
         f(x)=\begin{cases} 0.5x & x \le 2\\ 1.5-0.25x & 2<x<6 \\ 0.5 & x \ge 6 \end{cases} 
       
      
     f(x)=⎩
              ⎨
              ⎧0.5x1.5−0.25x0.5x≤22<x<6x≥6
 调用此函数并求解x=0、5、10时的函数值。
function y=f(x)
  if x<=2
    y=0.5*x;
  elseif x>2 & x<6
    y=1.5-0.25*x;
  elseif x>=6
    y=0.5;
  end
>> f(0)
ans = 0
>> f(5)
ans = 0.2500
>> f(10)
ans = 0.5000
4.编写一个脚本,查找给定字符串中指定字符出现的次数和位置。
function [c,p]=fs(str,s)
  p=find(str==s)
  c=length(p)
end
>> s='How much wood would a woodchuck chuck?';
>> fs(s,'h')
p =
    8   28   34
c = 3
ans = 3
5.编写一个脚本,判断输入字符串中每个单词的首字母是否为大写。若不是,则将其修改为大写,其他字母为小写。
%方法一
str='How much wood would a woodchuck chuck?'
s_length=length(str)
for k=1:s_length
    if(k==1||isspace(str(k-1)))&&(str(k)<='z'&& str(k)>='a')
        str(k)=char(double(str(k))-32);
    end
end
disp(str)%
>> daxiaoxie
                                                                                   
str = How much wood would a woodchuck chuck?
s_length = 38
How Much Wood Would A Woodchuck Chuck?
%方法二
a='How much wood would a woodchuck chuck?'
a1=lower(a);
i=isspace(a);
inx=[1,find(i)+1];
a1(inx)=upper(a1(inx));
%a1(inx)=(a(inx)-32);
方法二先将所有的字母转化为小写字母,然后找到空格的位置,然后再找到首字母的位置,再讲首字母大写。
6.创建2×2的单元数组,第1和第2个元素为字符串,第3个元素为整型变量,第4 个元素为双精度(double)类型。
% 直接创建法
>> A(1,1)={'abc'};
>> A(1,2)={'def'};
>> A(2,1)={int8(1)};
>> A(2,2)={double(5)};
>> A
A =
{
  [1,1] = abc
  [2,1] = 1
  [1,2] = def
  [2,2] = 5
}
7.创建一个结构体,用于统计10个学生的情况,包括学生的姓名、学号、英语成绩等。然后使用该结构体对一个班级的学生成绩进行管理,如计算总分、平均分、排列名次等。
student=struct('name',{'zhangsan','lisi','wangwu','Tom','Mike','John','guoyuan','liuneng','yutian','Alan'},
'no',{'001','002','003','004','005','006','007','008','009','010'},
'score',{[60 50 70],[70 80 90],[80 70 70],[66 75 80],[77 88 90],[80 90 60],[60 85 73],[68 87 76],[85 69 62],[60 55 78]});
%计算成绩总和、平均分
for i=1:10
  s(i)=sum(student(i).score);
  m(i)=mean(student(i).score);
end
s
m
%根据平均值进行排序,输出排序后的学号
[snew,index]=sort(m);
for i=index
  student(i).no
end
>> student_struct
s =
   180   240   220   221   255   230   218   231   216   193
m =
   60.000   80.000   73.333   73.667   85.000   76.667   72.667   77.000   72.000   64.333
ans = 001
ans = 010
ans = 009
ans = 007
ans = 003
ans = 004
ans = 006
ans = 008
ans = 002
ans = 005



















