MATLAB自定义函数并绘制
生活随笔
收集整理的這篇文章主要介紹了
MATLAB自定义函数并绘制
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
MATLAB在學習生活中應用非常廣,簡單總結一點MATLAB的使用小技巧,在腳本中自定義函數表達式并進行繪制
主要包括:
- 線性: f(x)=ax+b
- 二次:f(x)=ax^2+bx+c
- 高斯函數:
- 廣義高斯函數:
- 對數正態函數:
代碼實現的主要思路為:
繪制曲線圖為:
線性函數和二次函數:
函數表達式為:fx1 = x + 5 以及 fx2 = x^2 + 1
高斯函數與廣義高斯函數:
注意:alpha的取值決定了函數頂部的尖銳程度,當alpha<sqrt(2)時曲線很窄;alpha=sqrt(2)時為標準的高斯函數;alpha>sqrt(2)時,曲線的頂部變寬。
對數正態函數:
源代碼如下(若格式亂了也可以通過:此鏈接下載相關文件):
%% xiaochen wang 2020/07/11 % plot function curve % such as: % linear:f(x)=ax+b % quadratic:f(x)=ax^2+bx+c % gaussian function:f(x)=a*exp(-(x-μ)^2/2σ^2) % generalized gaussian function:f(x)=a*exp(-|x-μ|^α^2/2σ^2) % lognormal function:f(x)=a*exp(-(ln(x-s)-μ)^2/2σ^2)clc; clear all;syms x fx1 fx2 fx3 fx4_1 fx4_2 fx5; % creating symbolic variables or functiona = 20; miu = 0; xigma = 1; % define the parameters of gaussian function, A,μ,σ alpha = sqrt(4); % define the value of parameter α (generalized gaussian function) s = 1; % define the value of parameter s (lognormal function)fx1 = x + 5; % linear fx2 = x^2 + 1; % quadratic fx3 = a*exp(-(x-miu)^2/(2*xigma^2)); % gaussian fx4_1 = a*exp(-abs(x-miu)^(alpha^2)/(2*xigma^2)); % generalized gaussian alpha = sqrt(1); % modify the value of alpha fx4_2 = a*exp(-abs(x-miu)^(alpha^2)/(2*xigma^2)); % generalized gaussian fx5 = a*(1/(x*xigma*sqrt(2*pi)))*exp(-(log(x)-miu)^2/(2*xigma^2)); %1/x*xigma*sqrt(2*pi)xx = -5:0.01:5; %variables x xx2 = 0.01:0.01:5; %variables x in lognormal function must greater than zero ans1 = subs(fx1, x, xx); ans2 = subs(fx2, x, xx); ans3 = subs(fx3, x, xx); ans4_1 = subs(fx4_1, x, xx); ans4_2 = subs(fx4_2, x, xx); ans5 = subs(fx5, x, xx2);figure('Name', 'Linear_and_Quadratic'); plot(xx, ans1, 'r'); % plot linear function hold on; plot(xx, ans2, 'b'); % plot quadratic function legend('Linear', 'Quadratic');figure('Name', 'Gaussian_and_Generalized gaussian'); plot(xx, ans4_2, 'r'); % alpha = 1 hold on; plot(xx, ans3, 'g'); % alpha = sqrt(2) hold on; plot(xx, ans4_1, 'b'); % alpha = 2 legend('alpha=1', 'alpha=suqt(2)', 'alpha=2')figure('Name', 'Lognormal'); plot(xx2, ans5); legend('Lognormal');總結
以上是生活随笔為你收集整理的MATLAB自定义函数并绘制的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何优雅的使用迅雷(Mac)
- 下一篇: matlab函数作图格式,Matlab绘