MTATLAB 数据分析与可视化 Assignment1


某带学双创…

咳咳,大家都懂

有帮助的话点个赞收藏一下什么的

斐波那契数列求和

Fibonacci numbers are a sequence where the next number is the sum of the previous two numbers. Write a function that calculates the nth Fibonacci number.
Fibonacci numbers: 1 1 2 3 5 8 …
Given n, return f where f = fib(n) and f(1) = 1, f(2) = 1, f(3) = 2, …
Examples:
Input n = 5
Output f is 5
Input n = 7
Output f is 13

代码实现

clc;
n=input('Pls enter the number:');
y=f(n);
fprintf('Sum=%d',y);
function y=f(n)
a(1)=1;
a(2)=1;
for i=3:n
    a(i)=a(i-1)+a(i-2);
end
y=a(n);
end

求给定区间质数

Write a function that will compute and return all the prime numbers up to N. One way to solve this is using a simple loop and a conditional statement. However, any other correct solution is acceptable. You are not allowed to use Matlab’s inbuilt function for your answer. You can use it to check your answer only.
Examples;
Input N = 10; Output P = [2, 3, 5, 7]
Input N = 3; Output P = [2, 3]

代码实现

clc;
n=input('Pls enter the number:');
y=prmArray(n);
disp(y);
function y=prmArray(n)
ii=1;
if n==1
    fprintf('Pls enter a number more than 1!');
    return;
else
    for i=2:n
        if isPrmNum(i)==true
            y(ii)=i;
            ii=ii+1;
        end
    end
    return;
end
end

function isPrimeNumber=isPrmNum(n)
isPrimeNumber=true;
if n==2
    return;
else
    for i=2:n-1
    if mod(n,i)==0
        isPrimeNumber=false;
        return;
    end
    end  
end
return;
end

计算三角形网络面围成立体的表面积

A complex 3D surface can be represented by a triangular mesh which is a collection of planar 3D triangles as shown in the figure below.

三角网络图片示意
Write a function that finds the area of any 3D surface represented by as set of 3D points P (a Nx3 matrix) and triangles T (a Mx3 matrix) that contains the three vertices of the triangles (as row indices of the matrix P). The surface area is essentially the sum of all the triangle areas. Note that the triangles are in 3D.
Find the equation for the area of a triangle from this webpage http://mathworld.wolfram.com/TriangleArea.html.
Example: Three sides of a cube.
Input P = [0 0 0; 1 0 0; 1 1 0; 0 1 0; 0 1 1; 0 0 1; 1 0 1];
T = [1 2 3; 1 3 4; 1 4 5; 1 5 6; 1 6 7; 1 2 7];
Output
area = 3

代码实现

(需要在命令行窗口或工作区为P,T赋值)

clc
for i=1:size(T,1)
    trg=T(i,:);
    point_1=P(trg(1),:);
    point_2=P(trg(2),:);
    point_3=P(trg(3),:);
    area(i)=Trg_area(point_1,point_2,point_3);
end
fprintf('area=%f',sum(sum(area)));
function area=Trg_area(pt1,pt2,pt3)
mat_1=[pt2(1),pt3(1),1;pt2(2),pt3(2),1;pt2(3),pt3(3),1];
mat_2=[pt3(1),pt1(1),1;pt3(2),pt1(2),1;pt3(3),pt1(3),1];
mat_3=[pt1(1),pt2(1),1;pt1(2),pt2(2),1;pt1(3),pt2(3),1];
area=0.5*sqrt((det(mat_1))^2+(det(mat_2))^2+(det(mat_3))^2);
end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值